Java — Properties of a method-local inner class (what modifiers are allowed?)
-
AIt must be marked final.
-
BIt can be marked abstract.
-
CIt can be marked public.
-
DIt can be marked static.
Answer
Correct Answer: It can be marked abstract.
Explanation
Introduction / Context:This question verifies which modifiers are legal for a class declared within a method (a method-local inner class). These rules differ from those for member and top-level classes.
Given Data / Assumptions:
- A method-local inner class is declared inside a method body.
- It is implicitly associated with the enclosing instance at runtime.
Concept / Approach:Method-local inner classes cannot use access modifiers (public, private, protected) or static. They are implicitly non-static. However, they may be declared abstract or final (depending on design). The important takeaway: abstract is allowed; public and static are not.
Step-by-Step Reasoning:
public/private/protected: not permitted at method scope for class declarations.static: not permitted for a class declared inside a non-static context; method-local classes are inherently non-static.abstract: permitted; you can declare a method-local inner class abstract and subclass it anonymously or with another local class.Verification / Alternative check:Try compiling examples with each modifier to see which are accepted by the Java compiler.
Why Other Options Are Wrong:
- "Must be marked final" is false; final is optional.
- "public" and "static" are illegal for method-local classes.
Common Pitfalls:Assuming all inner classes can be static or use access modifiers; the rules depend on where the class is declared.
Final Answer:It can be marked abstract.