Arrays and Var-arg Types

Java 9 min min read Updated: Mar 31, 2026 Beginner
Arrays and Var-arg Types
Beginner Topic 23 of 25

Types of Objects and Garbage Collection

In Java, objects are the heart of object-oriented programming. Every time we create an instance of a class, an object is created in memory. These objects store data and allow methods to operate on that data.

As Java programs run, many objects are created, used, and eventually become unnecessary. Since memory is limited, Java must reclaim memory occupied by unused objects. This process is handled automatically through garbage collection.

Key Concept: Java objects are created in heap memory, and when they are no longer reachable, the JVM can remove them automatically through garbage collection.

What is an Object in Java?

An object is an instance of a class. It represents a real entity in memory and contains state and behavior.

Example:

java class Student { String name; int age; } public class Main { public static void main(String[] args) { Student s = new Student(); s.name = "Amit"; s.age = 20; } }

Here:

  • Student is a class
  • s is a reference variable
  • new Student() creates an object in heap memory

Where are Objects Stored?

In Java:

  • Objects are stored in heap memory
  • Reference variables may exist in stack or as fields depending on where they are declared

Example:

java Student s = new Student();

Here:

  • s is the reference
  • actual object is in heap memory

Types of Objects in Java

In Java, objects can be discussed in different ways based on how they are created and how they exist in memory. Common practical categories include:

  • Anonymous objects
  • Named objects
  • Referenced objects
  • Unreferenced objects
  • Eligible-for-garbage-collection objects
  • Immutable objects
  • Mutable objects

Let us understand them one by one.

1. Named Object

A named object is an object that is referenced through a variable name.

java Student s = new Student();

Here, the object has a reference variable s, so it is a named object.

2. Anonymous Object

An anonymous object is an object that is created without assigning it to a reference variable.

Example

java new Student();

This object is created but no reference variable stores it.

Anonymous objects are usually used when an object is needed only once.

Example with Method Call

java class Student { void show() { System.out.println("Anonymous object used"); } } public class Main { public static void main(String[] args) { new Student().show(); } }

Here, the object is created and immediately used to call show().

3. Referenced Object

Any object that is currently reachable through a valid reference variable is called a referenced object.

java Student s = new Student();

Since s points to the object, it is referenced and not eligible for garbage collection.

4. Unreferenced Object

An unreferenced object is an object that no longer has any valid reference pointing to it.

Once an object becomes unreferenced, it becomes eligible for garbage collection.

Example

java Student s = new Student(); s = null;

Here, the original object created by new Student() becomes unreferenced after s = null;.

5. Mutable Object

A mutable object is an object whose internal state can be changed after creation.

Example

java class Student { String name; } public class Main { public static void main(String[] args) { Student s = new Student(); s.name = "Amit"; s.name = "Riya"; } }

Since the value of name can change, the object is mutable.

6. Immutable Object

An immutable object is an object whose state cannot be changed after creation.

The most famous example in Java is the String class.

Example

java String s = "Java"; s = s.concat(" Programming");

Here, the original String object is not modified. Instead, a new object is created.

Object Creation in Java

Objects are most commonly created using the new keyword.

java Car c = new Car();

This process involves:

  1. memory allocation in heap
  2. default initialization of fields
  3. execution of instance variable initialization
  4. execution of instance blocks
  5. execution of constructor

Object Reference and Actual Object

Beginners often confuse the reference variable with the actual object.

java Student s = new Student();
  • s is not the object itself
  • s holds the reference to the object
  • the actual object lives in heap memory

Ways an Object Becomes Eligible for Garbage Collection

An object becomes eligible for garbage collection when it is no longer reachable by any live reference.

Common ways:

  • nullifying the reference
  • reassigning the reference
  • objects created inside methods becoming unreachable after method ends
  • isolated island objects
  • anonymous objects after use

1. Nullifying a Reference

java Student s = new Student(); s = null;

The object becomes unreachable because the reference now points to nothing.

2. Reassigning Reference

java Student s = new Student(); s = new Student();

The first object becomes unreferenced because s now points to a new object.

3. Local Objects After Method Completion

Objects created inside a method may become unreachable once the method completes, if no reference escapes the method.

java class Demo { void createObject() { Student s = new Student(); } }

After createObject() finishes, the local reference s is destroyed, so the object becomes eligible for garbage collection.

4. Anonymous Objects After Use

java new Student().show();

Once the statement finishes, the object usually becomes unreachable because no reference is stored.

5. Isolated Island of Objects

An isolated island happens when a group of objects reference each other but none of them is reachable from outside.

Example

java class A { B b; } class B { A a; } public class Main { public static void main(String[] args) { A a1 = new A(); B b1 = new B(); a1.b = b1; b1.a = a1; a1 = null; b1 = null; } }

Here, both objects still refer to each other, but no external reference points to them. Therefore, they become eligible for garbage collection.

What is Garbage Collection?

Garbage collection is the automatic memory cleanup process in Java. The JVM identifies objects that are no longer reachable and reclaims their memory.

This removes the burden of manual memory management from the programmer.

Why Garbage Collection is Needed

  • to free unused memory
  • to prevent memory waste
  • to reduce risk of memory leaks
  • to simplify development
  • to improve application stability

How Garbage Collection Works

The garbage collector works on the principle of reachability.

If an object is reachable through active references, it is kept alive.

If an object is unreachable, it becomes eligible for garbage collection.

Simple Rule

  • Reachable object → not collected
  • Unreachable object → eligible for collection

Can We Force Garbage Collection?

Java provides methods like:

  • System.gc()
  • Runtime.getRuntime().gc()

But these methods only request garbage collection. They do not guarantee that garbage collection will happen immediately.

Example

java public class Main { public static void main(String[] args) { Student s = new Student(); s = null; System.gc(); System.out.println("Requested garbage collection"); } }

What is finalize() Method?

The finalize() method was historically used to perform cleanup before an object was removed by the garbage collector.

Example:

java class Student { protected void finalize() { System.out.println("Object is being garbage collected"); } }

However, in modern Java, finalize() is considered outdated and should generally be avoided.

Important Note About finalize()

Even in older Java versions, finalize() was not guaranteed to execute immediately or at all before program termination. Therefore, it should not be relied upon for important resource cleanup.

Modern Java prefers:

  • try-with-resources
  • explicit close methods
  • cleaner resource management patterns

Example of Garbage Collection Flow

java class Demo { protected void finalize() { System.out.println("Garbage collected"); } } public class Main { public static void main(String[] args) { Demo d1 = new Demo(); Demo d2 = new Demo(); d1 = null; d2 = null; System.gc(); } }

This program requests garbage collection after making both objects unreachable.

Garbage Collection is Automatic, Not Manual Memory Freeing

In Java, programmers do not free object memory directly like in C or C++. Java automatically handles memory cleanup through garbage collection.

This is one of the reasons Java is considered safer and easier for large-scale development.

Advantages of Garbage Collection

  • automatic memory management
  • reduced memory-related bugs
  • less risk of dangling pointers
  • simpler development process
  • better safety compared to manual memory management languages

Limitations of Garbage Collection

  • exact collection time is not guaranteed
  • programmer has less low-level control
  • poor object handling can still cause memory pressure
  • unreleased external resources are not automatically managed like normal heap objects

Common Mistakes

  • Thinking object reference and actual object are the same
  • Believing System.gc() guarantees immediate collection
  • Assuming two objects referencing each other can never be garbage collected
  • Confusing memory cleanup with resource cleanup like files or DB connections
  • Overusing objects unnecessarily in loops and large operations

Best Practices

  • Create objects only when needed
  • Remove unused references when appropriate
  • Avoid holding references longer than necessary
  • Use immutable objects where beneficial
  • Manage external resources using try-with-resources instead of relying on garbage collection

Interview-Oriented Points

  • Objects are stored in heap memory
  • Anonymous objects have no reference variable
  • Mutable objects can change state, immutable objects cannot
  • Garbage collection removes unreachable objects automatically
  • Objects become eligible for garbage collection when no live reference points to them
  • System.gc() only requests garbage collection
  • Isolated island objects are eligible for garbage collection
  • finalize() is outdated and should generally be avoided

Conclusion

Objects are the core runtime entities in Java, and understanding their lifecycle is essential for becoming a strong Java developer. Knowing how objects are created, referenced, modified, and eventually removed gives a much deeper understanding of Java memory behavior.

Garbage collection is one of Java’s most powerful features because it automatically reclaims memory from unused objects. This makes Java safer and easier to use than many low-level languages, especially in large applications.

Quick Summary: Java objects are created in heap memory and become eligible for garbage collection when they are no longer reachable. The JVM automatically reclaims memory from such unused objects.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators