Instance vs Object vs Reference in Java with Example

In order to understand the instance vs object vs reference in Java. First, we need to understand What is Object and Memory Management in Java.

1. What is Object and How it is created?

I guess you have enough knowledge of Object-oriented programming and Classes in Java.

Therefore, now we are going to look at different ways to create objects in Java.

There are many different ways but we will look at the top 3 ways to create an object in Java.

1.1. Using a new Keyword: This is the very basic way to create an object and almost 99% of objects are created using the new keyword. Let’s take an example:

// using new keyword 
public class Example  
{ 
    String name = "MasterInCoding"; 
    public static void main(String[] args)  
    { 
        // Creating Object of  
        // Example class using new keyword 
        Example obj = new Example(); 
        System.out.println(obj.name); 
    } 
}

1.2. Using the newInstance() method: 

We can also create an object using Class.forName(“ClassName”) and  newInstance() method. This way we can create Java objects dynamically.

public class Example{ 

	String name = "MasterInCoding";

	public static void main(String[] args) {
		try {
			Class cls = Class.forName("Example");
			Example obj = (Example) cls.newInstance();
			System.out.println(obj.name);
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} 
		catch (InstantiationException e) {
			e.printStackTrace();
		} 
		catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
}

1.3. Using clone() method: As the name clarifies “clone”. By using this method, when clone() is called, JVM copies all content from the previous object and creates a new copy of that object.

For using this method we need to implement Cloneable interface.

public class Example implements Cloneable {
	@Override
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}

	String name = "MasterInCoding";

	public static void main(String[] args) {
		Example obj1 = new Example();
		try {
			Example obj2 = (Example) obj1.clone();
			System.out.println(obj2.name);
		} 
		catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
	}
}

You have seen different ways to create Object now we will look into the memory part of Java.

2. What is Object in Java?

We will take an example with the new keyword and try to understand what happens inside memory when we use the new keyword to create an object.

Example:

public class Student
{ 
    int i = 10;
    public static void main(String[] args)  
    {  
        Student stu = new Student(); 
    } 
}
  • Here, a new instance of a Student class is created.
  • Space is allocated for new Student() to hold the field “i“.
  • Fields will be initialized with their default values.
Java_Object

3. What is Reference in Java?

As discussed above, when we use the new keyword an object is created. Also, the memory reference of that object is stored in the reference variable.

Therefore, we named it as a reference because it contains the actual reference or address of the object in the memory.

Student stu = new Student();

Here, the Student object is created and its reference is stored in the stu variable.

Always Remember:

  • stu contains the reference to access the object.
  • Inside RAM, when we create an object it’s always stored in the heap space.
  • Stack memory contains the reference to the object.
  • Java automatic garbage collector runs on heap memory.
  • When stack memory is full, Java throws java.lang.StackOverflowError.
  • When heap memory is full, Java throws java.lang.OutOfMemoryError.

4. What is an Instance in Java?

We often use objects and instance word, but sometimes it confuses beginners like anything.

Let me clear you one thing, these both words are the same don’t get confused.

Now let me answer your question then why sometimes we call objects and sometimes instance?

When we create an object in Java, we are creating an instance of any class.

That means that class is present there and you can access the methods and variables of that class.

Therefore, sometimes we also call it an instance.

Student stu = new Student();

Here, stu is an object name also an instance variable because it holds the memory address/reference of the object created in heap memory.

I hope you’ve understood Instance vs Object vs Reference in Java. Let’s move on to the next chapter.