Hussein AlHroub
Hussein AlHroub

Reputation: 33

Java What will happen if we assigned two objects to the same variable

In java, lets say I have executed this code

ClassA classa = new ClassA();
classa = new ClassA();

What will happen to the old object? will it be deleted, or it will stick in memory?

Upvotes: 0

Views: 81

Answers (1)

AR1
AR1

Reputation: 5005

The classa variable will point to the new object ClassA. The old ClassA object will be eligible for Garbage Collection. You can find this behaviour documented in this garbage collector document.

Upvotes: 4

Related Questions