An object is collected once no reachable reference points to it. The variable going out of scope does not destroy the object, it only removes one path to it.
public static void main(String[] args) {
BankAccount temporary = new BankAccount("ACC-9999");
temporary.deposit(FIFTY);
// the object is now unreachable, so it is eligible for collection
}
Eligibility is not the same as collection. The garbage collector runs whenever the JVM decides to, which is why you should never rely on finalisation timing or expect an object to vanish at the end of a block. The memory management topic covers the collectors and the areas in detail.
Frequently Asked Questions
What exactly is an object in Java?
An object combines state stored in its own fields, behavior provided by methods defined in its class, and an identity that makes it distinct from every other object. The object itself lives on the heap. Any variable you write is only a reference value pointing at it, never the object itself.
What does the new keyword actually do?
It performs three steps in order. It allocates heap memory for the object, it invokes the matching constructor so the new object has valid initial state, and the whole expression then evaluates to a reference value. The variable receives that reference, not the object.
Where are variables stored and where are objects stored?
Local variables of a method live on the stack for the duration of that call, while objects live on the heap until they become unreachable and are collected. The reference value held by a stack variable is what connects the two. The full picture, including the metaspace, is covered in the memory management topic.
Why does == return false for two equal strings?
Because == compares identity rather than content, and the two objects were created separately so they are different objects. equals compares contents and returns true. When you assign string literals the JVM usually reuses one pooled instance, so two literals can be identical by accident, which is why == is worth avoiding outside null checks.
Does an object get destroyed when a variable goes out of scope?
No. Losing a reference only removes one path to the object. It becomes eligible for collection once no reachable reference points to it, and the collector may reclaim it much later, so you must never depend on an object disappearing at the end of a block or on finalisation timing.
Does assigning a variable create a copy of the object?
No. It copies the reference, so both names point at one object and mutating through either is visible through the other. To get an independent object you must construct a new one, either explicitly or with a copy constructor, and for deep copies of nested references you need more care.
Why does an object with no fields still use memory?
Every object carries a header holding the class pointer, identity hash code, garbage collection age, and lock state, and then padding to align the total. An object with no fields is essentially just that header, which is why large arrays of tiny objects cost so much more than primitive arrays of the same length.
Should I declare fields final?
Declare a field final whenever the value cannot change after construction, which is most of the time. A final field cannot be reassigned, which makes the object easier to reason about and lets the JVM optimise more aggressively. Reserve non-final fields for values that genuinely need to change after construction, and keep them private so changes pass through a method that validates them.