Instantiation is a universal concept in Java programming which refers to the process of creating an object of a class. It takes up the object’s initial memory space and returns a reference. The blueprint for the class is provided by an object instantiation. We can create an unlimited number of class objects to represent user-defined data such as lists.
This article will explain the method related to the instantiation of objects in Java.
How to Instantiate an Object in Java?
Instantiation is the process of constructing a class object. That’s why an object is also called the instance of a Java class. In Java, we can make instances of a class by utilizing the “new” keyword.
Syntax
The syntax to instantiate the object of a class:
Let’s see the examples of object instantiation in Java.
Example 1: Instantiate a Single Object in Java
Here, we have a class named “JavaClass” with variables “x”, “y”, a user-defined method “Sum()”, and the predefined “main()” method:
int x,y;
private int Sum() {
x=5;
y=11;
return x+y;
}
We will create an instance or object of this class named “jc” in the main() method by using a “new” keyword. Using this object, we will access the “Sum()” method and store the returned value in the “ans” int type variable. Lastly, utilize the “System.out.println()” method to print out the sum at the console:
Output
Example 2: Instantiate a Single Object in Java Using Multiple Classes
We can also create an object of one class into another class and access the public methods of that class. In this example, we have two classes: “JavaClass1” and “Example”.
“JavaClass1” contains a method named “Message()” and a String type variable “name”:
We will create an object of the class JavaClass1 in the main method of the class Example and access all the public methods of the JavaClass1 in the second class named Example.
Here, we call the method of JavaClass1 in main method of the Example class by using object “jc”:
public static void main(String[] args) {
JavaClass1 jc = new JavaClass1();
jc.Message();
}
}
Output
Example 3: Instantiate Multiple Objects in Java Using Multiple Classes
We can also create multiple objects of the same class. In this example, we have the same two classes as in the above example. Now we will create multiple objects of the class JavaClass1 in the main method of the second class Example.
“Javaclass1” contains a constructor, two user-defined methods, and two variables. In the constructor, we will assign the reference variables to the global variables of the class. Whereas, the “Sum()” and the “sub()” methods returns the sum and differences of the “x” and “y” variables:
int x,y;
public JavaClass1(int a,int b) {
x=a;
y=b;
}
int Sum() {
return x+y;
}
int sub() {
return x-y;
}
}
In the main method of the class Example, we will create two objects of the “JavaClass1” as “jc” and “jc1” by passing integer values as arguments. The constructor instantiates the class variables with the given values. Lastly, we will access all the “Sum()” method will “jc” object and “sub()” with “jc1”:
Output
We have compiled all of the basic information related to instantiating an object in Java.
Conclusion
In Java, you can instantiate or create an object of the class by utilizing the “new” keyword. The instance of a Java class is another name for an object. You can create an object of the same class or of another class to access their member functions. You can also instantiate multiple objects using multiple classes. In this article, we explained the method to instantiate an object in Java.