Object life cycle in Java

Earlier, we discussed the Java objects and classes, and now we will talk about the object life cycle in Java. An object’s life cycle starts when it’s created until it goes out of scope or dereferenced.

Various stages in the object’s life cycle –

Let’s look at each stage one by one.

Object is created

An object is created by using the “new” keyword operator. And then, we can initiate a reference variable with this object.

But there is a difference between declaring a reference variable and initializing it.

class Person{}

class Main{

  Person person;  // declaring a variable
}
class Person{}

class Main{

   // declaring and 
  // initializing the variable
  Person person = new Person();  
}
  • The left-hand side code has only declared a reference variable person of type Person, and no object is being created here.
  • And the right-hand side code has not only declared a variable but also instantiated it.

**String is an exceptional class. Strings can be initialized using the literal strings that do not require the new keyword operator.

// created a string using the literal string
String str = "codekru" 
// created a string using the new keyword
String str = new String("codekru")

But initializing a reference variable using the literal strings doesn’t always result in creating a new object, but initializing it using a new keyword will always create a new object.

Object is accessible

Once an object is created, it can be accessed using its reference variable. It remains accessible till it goes out of scope or is unreferenced.

class Person{}

class Main{

  new Person();  // unreferenced object
}
class Person{}

class Main{

  Person person = new Person();  // referenced object
}

Now, the person variable would be able to access the Person’s class member variable and its functions depending on the access modifiers.

Let’s see things with an example. We will take an Employee class with only a single property ( “name” ) and then access its property via the reference variable.

class Employee {
	String name;
}

public class Codekru {

	public static void main(String[] args) {

		// initializing the reference variable
		Employee e1 = new Employee();

		// accessing the properties using reference variable
		e1.name = "John";

		// again, accessing the properties
		System.out.println("Employee1 name: " + e1.name);

		// initializing another reference variable
		Employee e2 = new Employee();

		e2.name = "Alex";

		System.out.println("Employee2 name: " + e2.name);

	}

}

Output –

Employee1 name: John
Employee2 name: Alex
  • Here, we have created an Employee object and accessed its properties via the reference variable e1.
  • And then, we created another Employee object and accessed its properties via the reference variable e2.

Object is inaccessible

An object can become inaccessible if it goes out of scope or is dereferenced.

Out of Scope

If a reference variable is made inside a block, it only exists within that block. It can’t be accessed outside of the block.

class Employee {
	String name;
}

public class Codekru {

	public static void main(String[] args) {

		int age = 24;
		
		if(age > 25) {
			Employee e1 = new Employee();
			// accessing the properties using reference variable
			e1.name = "John";
		}

		 // this will give error
		// as e1 is not accessible outside the if condition
		System.out.println(e1.name);

	}

}

Now, e1 was not accessible outside the block as its scope was only confined within the if block. And thus, the object referred by the e1 reference variable is also not accessible and is eligible to be collected by the garbage collector.

Dereferenced

There can be another scenario where an object is not accessible. It’s when the reference variable points to another instance, and the old instance is thus unreachable.

class Employee {
	String name;
}

public class Codekru {

	public static void main(String[] args) {

		Employee e1 = new Employee();
		e1.name = "John";

		e1 = new Employee(); // referencing to a new object
		System.out.println(e1.name);

	}

}

Output –

null

Here, the e1 reference variable is pointing to a new Employee instance, and the old instance can no longer be referenced. Thus, there is no way left to reach that instance anymore, so it will be eligible to be collected by the garbage collector.

Garbage collector

Unlike any other language, such as C, Java handles all the memory management. It uses the garbage collector to reclaim unused memory. The garbage collector in Java is a low-priority thread that runs periodically and frees up any unused space.

We can only determine which objects are eligible to be garbage collected, but we cannot determine when they will be garbage collected. The JVM handles it, and programmers do not have control over it.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *