Skip to main content

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:
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

Popular posts from this blog

Advantages & Disadvantages of Synchronous / Asynchronous Communications?

  Asynchronous Communication Advantages: Requests need not be targeted to specific server. Service need not be available when request is made. No blocking, so resources could be freed.  Could use connectionless protocol Disadvantages: Response times are unpredictable. Error handling usually more complex.  Usually requires connection-oriented protocol.  Harder to design apps Synchronous Communication Advantages: Easy to program Outcome is known immediately  Error recovery easier (usually)  Better real-time response (usually) Disadvantages: Service must be up and ready. Requestor blocks, held resources are “tied up”.  Usually requires connection-oriented protocol

WebSphere MQ Interview Questions

What is MQ and what does it do? Ans. MQ stands for MESSAGE QUEUEING. WebSphere MQ allows application programs to use message queuing to participate in message-driven processing. Application programs can communicate across different platforms by using the appropriate message queuing software products. What is Message driven process? Ans . When messages arrive on a queue, they can automatically start an application using triggering. If necessary, the applications can be stopped when the message (or messages) have been processed. What are advantages of the MQ? Ans. 1. Integration. 2. Asynchrony 3. Assured Delivery 4. Scalability. How does it support the Integration? Ans. Because the MQ is independent of the Operating System you use i.e. it may be Windows, Solaris,AIX.It is independent of the protocol (i.e. TCP/IP, LU6.2, SNA, NetBIOS, UDP).It is not required that both the sender and receiver should be running on the same platform What is Asynchrony? Ans. With messag

XML Binding with JAXB 2.0 - Tutorial

Java Architecture for XML Binding (JAXB) is an API/framework that binds XML schema to Java representations. Java objects may then subsequently be used to marshal or unmarshal XML documents. Marshalling an XML document means creating an XML document from Java objects. Unmarshalling means creating creating a Java representation of an XML document (or, in effect, the reverse of marshaling). You retrieve the element and attribute values of the XML document from the Java representation. The JAXB 2.0 specification is implemented in JWSDP 2.0. JAXB 2.0 has some new features, which facilitate the marshalling and unmarshalling of an XML document. JAXB 2.0 also allows you to map a Java object to an XML document or an XML Schema. Some of the new features in JAXB 2.0 include: Smaller runtime libraries are required for JAXB 2.0, which require lesser runtime memory. Significantly, fewer Java classes are generated from a schema, compared to JAXB 1.0. For each top-level complexType, 2.0 generates a v