new keyword in java

What is new Keyword in Java? | New Keyword in java

Here, Firstly we will understand the concept of new keyword in java with real world, and then we will connect it with our topic.

Suppose we have a Machine which has a Switch to Press(i.e. Figure 1) and The number of times we press it, it makes something means If we press the switch two times, It will give us two product.

Figure 1

So, Now we will implement this same analogy with our new keyword and class in our Java Programs.

Here Machine will be replaced to Class, Switch will be replaced to new keyword and Product will be replaced to Object, So Figure(i.e. figure 2) will look like:

Figure 2

Here This figure explains that new is just like a switch in class that creates objects. The Number of times we use a new keyword, the same number of objects will be created in a class.

For Example, Look at this code:

public class A {  //Class Boundary starts
	public static void main(String[] args) {
		new A(); // First Object created
		new A(); // Second Object Created
	}

}//Class Boundary ends

Class Boundary: Here class boundary means the scope of the class in a program. In Java, To write any program we have to create at least one or more than one class and class boundary defines which statements belong to which class?

So, Now suppose Your Grandfather has kept some money and he said you to take that money and spend it for your wish but he didn’t tell you where he has kept that money. What will you do? Of course, You can not search because you don’t know the address or location where money has been kept.

This same analogy applies in the case of object creation.

Look at the below code, You can say that the Objects are created but where they have been created? to know any object’s address in our program, We have to use a reference variable with class type so we can store our object’s address in that variable.

public class A {
	public static void main(String[] args) {
	A a1=new A(); //a1 is a reference variable that stores object address

	}

}

Here, It has a reference variable a1 with class type and it is storing the object address. Now we know the location of our object that where it is created. To find the object address of an object, You can run the below code:

public class A {
	public static void main(String[] args) {
	A a1=new A();
	A a2=new A();
	
	System.out.println("The Address of first Object is: "+a1);
	System.out.println("The Address of Second Object is: "+a2);
	}

}

Keyword: A keyword in java means It has some predefined meaning

What new keyword does in our program?

  • It sends requests to the class to create objects.
  • Once Object is created, It gets the address of the object and stores it in its reference variable.

Note: For different objects, Object addresses will be different and it keeps changing every time it gets executed.

public class A {
	public static void main(String[] args) {
	A a1=new A();
	A a2=new A();
	A a3=new A();
	
	System.out.println("The Address of first Object is: "+a1); 
	System.out.println("The Address of Second Object is: "+a2);
	System.out.println("The Address of Third Object is: "+a3);
	}

}

Output:

The Address of first Object is: A@15db9742
The Address of first Object is: A@6d06d69c
The Address of first Object is: A@7852e922

On the above program output, All three Objects addresses are different from each other.

Class: It helps us to create objects.

Object: It is an instance of the class.

public class A {
	public static void main(String[] args) {
	A a1=new A();
	System.out.println("The Address of first Object is: "+a1);
	}

}

On the above code, These are the steps that have been followed:

  • Step 1: new sends a request to class A for Object creation.
  • Step 2: Object(i.e. new A()) has been created
  • Step 3: new gets Object address of new A();
  • Step 4: Object address is stored in reference variable a1
  • Step 5: it prints the value of a1, Which is objects address

Loading