Why it doesn't compile when an inner class has a method of the same name (but different signature) as that of its enclosing class?
--
The compiling the following code will generate an compilation error: public class Program { class Inner { void method(int i) {}; void doSomething() { method(); //compiler error } } void method() {} }
Determining the method that will be invoked by a method invocation expression involves several steps:
- 15.12.1 Compile-Time Step 1: Determine Class or Interface to Search
- 15.12.2 Compile-Time Step 2: Determine Method Signature
- 15.12.3 Compile-Time Step 3: Is the Chosen Method Appropriate?
According to the rules in 15.12.1 Compile-Time Step 1: Determine Class or Interface to Search, the first step is to figure out the name of the method to be invoked and which class or interface to check for definitions of methods of that name. No method parameters consider in this step:
The first step in processing a method invocation at compile time is to figure out the name of the method to be invoked and which class or interface to check for definitions of methods of that name. There are several cases to consider, depending on the form that precedes the left parenthesis, as follows:
- If the form is MethodName, then there are three subcases:
- If it is a simple name, that is, just an Identifier, then the name of the method is the Identifier. If the Identifier appears within the scope (6.3) of a visible method declaration with that name, then there must be an enclosing type declaration of which that method is a member. Let T be the innermost such type declaration. The class or interface to search is T.
- ..........
- ...
In our case, the Identifier is method method and the type T is the inner class Program.Inner. All inherited methods from superclass are included in such matching list but methods from enclosing classes are not in the matching list. The list consists only void method(int i) which is defined in Inner class.
According to 15.12.2 Compile-Time Step 2: Determine Method Signature, "The second step searches the type determined in the previous step for member methods. This step uses the name of the method and the types of the argument expressions to locate methods that are both accessible and applicable, that is, declarations that can be correctly invoked on the given arguments". In our case, the compiler realizes that the parameters don't match any methods provided by the first step and generates an error. The list consists only void method(int i) which is defined in Inner class and that does not match the required signature.
How can I call void method() in the enclosing class? Every inner class must be associated with an instance of its enclosing class. In fact, every inner class has an implicit reference to that enclosing class. That implicit reference takes the form "EnclosingClass.this". In our case, we can use Program.this.method() to fix this problem:
public class Program { static String str = "Hello from Program"; class Inner { static String str = "Hello from Inner"; void method(int i) {}; void doSomething() { System.out.println(Program.this.str); //Hello from Program Program.this.method(); } } void method() {} }
Comments
Post a Comment