Posts

Showing posts from September, 2022

Stubs & Drivers in Programming

  Sometimes, while creating a project it may happen that you want to test current functionality of the program, but that cannot be done because all of the required methods or functions have not been created or some of the functionality is still missing. Lets take a look into Stub. What is a Stub? A stub is a method with few or no code(only the declaration exists, no Implementation). The stub usually contains the default return value and parameters(which may or may not change in the future). The purpose of the stub is to let the program execute with default values in most of the places. By this you can check if the program works correctly or not. How it works? suppose I have a function which gets data from database but I haven’t written the code for fetching the data. I have another function display() which calls the method getData() and displays the returned arrayList to the User. But, IDK if display() function is working or not because the function getData() does not exist right n...

Create a jar/exe file in java using cmd

Image
  Yes, obviously why would someone create a jar file using console. When instead they could use an IDE. well, You would do it. That is why you are here. so, lets get started. To Create an executable file : Create a java file. Just like below. import javax.swing.JFrame; import javax.swing.JLabel;public class TempSwing { public static void main(String args[]) { JFrame frame = new JFrame(); frame.setTitle("Create Account"); JLabel G1 = new JLabel("Hello World!"); frame.setSize(500,700); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(G1); frame.setVisible(true); } } Contents of the folder should look Something like this. Now, open your console. and type in this command. jar -cfe Temp.jar TempSwing "." C — for creating a new jar file. f — for name of the jar file. e — name of the class file to be executed on double click. “.” — The .(dot) is used to compile all the files in the folder in the jar. This is how the folder should l...

Anonymous/Unnamed code blocks in Java

  Anonymous code blocks in java are used to write common code for every constructor of a class. Syntax for the anonymous code blocks looks like this : { //Common code goes here. } These blocks are called as instance initialization blocks. These blocks don’t have any name and are called for every constructor in the class. These blocks are called before a constructor is called. Here’s an example class Demo{ public Demo(){ System.out.println("default constructor"); } public Demo(int i){ System.out.println("parameterized constructor"); } { System.out.print("Object is created by the "); } public static void main(String arr[]){ Demo b1 = new Demo(); Demo b2 = new Demo(1); } } Output of the code: Object is created by the default constructor Object is created by the parameterized constructor In the above you example you can see that the print statement in the anonymous block was called before calling the constructor for each constructor in ...