Difficulty: Medium
Correct Answer: An anonymous class is a local class without a name that is declared and instantiated in a single expression, often used to create one off implementations of interfaces or subclasses.
Explanation:
Introduction / Context:
Anonymous classes are a feature of Java that allow you to declare and instantiate a class at the same time without giving it a name. Before the introduction of lambda expressions, anonymous classes were widely used for event handlers, callbacks, and small customizations. Interviewers still ask about anonymous classes because they appear frequently in legacy code and illustrate how Java supports concise implementations of interfaces and abstract classes.
Given Data / Assumptions:
Concept / Approach:
An anonymous class is essentially a local class without a name that you define and instantiate at the point of use. The syntax looks like new InterfaceOrClass() { override methods here };. Because it has no textual name, you cannot refer to the anonymous class type elsewhere in the source; you interact with it through the reference type it extends or implements. Anonymous classes are convenient for defining one off behavior, such as a button click listener or a Comparator implementation, without cluttering the code with separate named classes. They can contain methods, fields, and initialization blocks like any other class but are scoped to the expression where they are created.
Step-by-Step Solution:
Step 1: Choose a class to extend or an interface to implement, for example Runnable or ActionListener.
Step 2: Create an object with syntax such as new Runnable() { public void run() { /* code */ } }.
Step 3: The braces define the body of the anonymous class, where you override necessary methods and possibly define additional fields.
Step 4: Assign this new object to a variable of the appropriate type or pass it directly as a method argument.
Step 5: Use the resulting object as an instance of the interface or superclass, while the concrete anonymous class remains unnamed in the source.
Verification / Alternative check:
If you compile code containing an anonymous class and then inspect the compiled files, you will see extra class files with names like Outer$1.class or Outer$2.class. These correspond to anonymous classes even though they have no source level names. Tools like IDEs and debuggers show that the runtime type of such objects is the anonymous class, but your code treats them as instances of the interface or superclass. This proves that anonymous classes are real classes with generated names, not just syntactic sugar that disappears entirely at compile time.
Why Other Options Are Wrong:
Option B is incorrect because a public class declared at the top level with or without a package is still a named class. Option C is wrong because anonymous classes can contain methods and fields; they are not comments or empty shells. Option D is incorrect because anonymous classes are not loaded at JVM startup in any special way; they are loaded when the code that uses them is executed, and their names are not fixed in source but generated by the compiler.
Common Pitfalls:
Common pitfalls include creating very large or complex anonymous classes that hurt readability; in such cases, a named class is usually better. Another issue is confusion when debugging, since the generated internal names Outer$1 and similar may not immediately indicate which anonymous class in source they represent. Since Java 8, many simple anonymous class use cases can be replaced by lambda expressions when targeting functional interfaces, which leads to shorter and clearer code while still allowing anonymous behavior.
Final Answer:
An anonymous class in Java is a local class without a name that is declared and instantiated in a single expression, typically to provide a one off implementation of an interface or subclass of an existing class.
Discussion & Comments