Journey of Java Object

Have you ever wondered what happens when you create an object of a class in Java? Does the object reside in the variable you create? Who deletes the object when its usage is done? In this blog, I’m going to address these questions.

Let’s consider the example below.


1.  class Student {
2.      private int rollNumber;
3.      private String name;

4.      public void setRollNumber(int rollNumber) {
5.          this.rollNumber = rollNumber;
6.      }

7.      public int getRollNumber() {
8.          return this.rollNumber;
9.      }

10.     public void setName(String name) {
11.         this.name = name;
12.     }

13.     public String getName() {
14.         return this.name;
15.     }
16. }

17. class Main {
18.     public static void main(String[] args) {
19.         Student s1 = new Student();
20.         s1.setRollNumber(10);
21.         s1.setName("John");
22.         System.out.println(s1.getRollNumber());
23.         System.out.println(s1.getName());
24.         System.out.println("------------");

25.         Student s2 = s1;
26.         s2.setName("Sam");
27.         System.out.println(s1.getRollNumber());
28.         System.out.println(s1.getName());
29.         System.out.println(s2.getRollNumber());
30.         System.out.println(s2.getName());
31.     }
32. }

We created a Student class that contains two variables: roll number and name. Each of these variables has its corresponding setter and getter methods to modify their values and access their current values. In the Main class, we have the main method, which firstly creates a Student object “s1” and then calls the setter method to set the value of the roll number to 10 and the name to “John”. Next, it prints the values of s1’s roll number and name. On line 25, we create s2 and assign the value of s1 to s2. Next, we modify the value of the name on s2 to “Sam”. We then print the values of s1’s and s2’s roll number and name.

According to you, what should be the output of the program? If you think it’s


10
John
------------
10
Sam
10
Sam

Then, it is correct, and you probably understand how Java manages objects.

Let’s examine the object creation and deletion process in detail now! The execution of a program in Java begins with loading the main function onto the stack. It then executes each instruction one after the other. In our case, execution begins from line #19.


Student s1 = new Student();

Here, using the new keyword allocates memory for the Student type on the Heap. The memory block includes space for int (rollNumber) and String (name). Below is the visual representation, assuming the memory address of the allocated block is 1000.

alter-text Visual depiction of Object allocation in Memory

s1 is a reference variable capable of storing only a memory address and utilizes the dot (.) operator to access the roll number and name of the memory block at address 1000. Lines #20 and #21 set the value of the roll number and name at the memory block pointed to by s1. After the execution of these lines, the visual representation looks like the following:

alter-text Visual depiction of Object in Memory after execution of line #21

On lines #22 and #23, we are requesting to print the values of the roll number and name stored in the memory block referenced by s1, i.e., the memory block with address 1000. Below is the program output after the execution of lines #22, #23, and #24.


10
John
------------

On line #25, we create another reference variable, s2, and assign its value to be that of s1, i.e., assigning the value 1000 to s2.

alter-text Visual depiction of Object in Memory after execution of line #25

Notice that both s1 and s2 are now referencing the same memory block! Therefore, changes made via any of these reference variables will affect the same memory block. On line #26, s2 modifies the value of the name stored at the memory block, 1000. Hence, the value changes from “John” to “Sam” at memory location 1000.

alter-text Visual depiction of Object in Memory after execution of line #26

On lines #27 and #28, we are requesting to print the values of the roll number and name stored at the memory location referenced by s1, i.e., 1000.


10
John
------------
10
Sam

Finally, on lines #29 and #30, we are requesting to print the values of the roll number and name stored at the memory location referenced by s2, i.e., 1000.


10
John
------------
10
Sam
10
Sam

After line #30, since there are no more instructions left to execute, the main method will be removed from the stack, and the execution of the program stops. As no part of the program is actively referencing memory block 1000, it will be marked for deletion by the Garbage Collector (a part of JRE). The memory block will be released for use by other processes.

alter-text Memory released for further usage

That’s it! If you find this blog useful, consider liking it. If you have any questions, please comment below, and follow me for more such blogs.

If you prefer watching videos, check out my YouTube channel at https://www.youtube.com/@gogettergeeks. For more information about me and my initiative, visit my website at https://www.gogettergeeks.com/.