Java

How to Instantiate an Object in Java

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:

Classname objectname = new Classname();

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:

public class JavaClass {
    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:

  public static void main(String[] args) {
        JavaClass jc = new JavaClass();
        int ans= jc.Sum();
        System.out.println("Sum of two numbers 5 and 11 is: " + ans);
    }
}

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”:

class JavaClass1 {
    String name;  
    void Message()  
    {  
        System.out.println("JavaClass1 is called.");  
    }  
}

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 class Example {
    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:

class JavaClass1 {
    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”:

public class Example {
    public static void main(String[] args) {
        JavaClass1 jc = new JavaClass1(6,9);
        JavaClass1 jc1 = new JavaClass1(19,2); 
        int ans= jc.Sum();
        int ans1= jc1.sub();   
        System.out.println("Result: " + ans);
        System.out.println("Result: " + ans1);
   }
}

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.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.