Module: | Core Architecture, Basics & Control Flow
Q16: Consider the following statements regarding Anonymous Objects in Java:
1. An anonymous object is fundamentally instantiated without assigning it to any reference variable, rendering it immediately eligible for garbage collection after its single operational use.
2. Anonymous objects are highly recommended for complex enterprise scenarios where an object's state must be preserved, maintained, and repeatedly updated across multiple independent method calls.
3. The specific syntax 'new Employee().calculateSalary();' represents a perfectly valid creation and instantaneous utilization of an anonymous object in a single execution statement.
Which of the above statements is/are correct?
2. Anonymous objects are highly recommended for complex enterprise scenarios where an object's state must be preserved, maintained, and repeatedly updated across multiple independent method calls.
3. The specific syntax 'new Employee().calculateSalary();' represents a perfectly valid creation and instantaneous utilization of an anonymous object in a single execution statement.
Which of the above statements is/are correct?
✅ Correct Answer: B
🎯 Quick Answer:
The correct combination is 1 and 3. Statement 2 is incorrect because anonymous objects physically cannot maintain state across multiple method calls. Since there is no reference variable pointing to the object, it is impossible to access it a second time to read or update its state.It exists solely to execute a single task or to be passed instantly as an argument to a method.
Structural Breakdown: Standard instantiation requires a reference: 'Employee emp = new Employee(); emp.calculateSalary();'. Anonymous instantiation compresses this: 'new Employee().calculateSalary();'. Historical/Related Context: Anonymous objects are deeply ingrained in Java's graphical user interface (GUI) frameworks like AWT and Swing, as well as modern event-driven architectures.
Developers frequently pass anonymous event listener objects directly into button-click methods because the listener object is never needed again elsewhere in the code.
Causal Reasoning: Eligibility for garbage collection is strictly determined by reference counts.
Because an anonymous object is never assigned a reference variable in the Stack memory, its reference count instantly drops to zero the millisecond its invoked method finishes executing, flagging it for immediate destruction by the JVM to save memory.