Create an instance of a local inner class

case I

 class Outer {    

    void outerMethod() {
        Inner i = new Inner();    //compile error : cannot find symbol
        i.innerMethod();

        class Inner {
            void innerMethod() {
                System.out.println("inner class method...");
            }
        }       
    }
}


  case II

class Outer {    

    void outerMethod() {
        Inner i = new Inner();
        i.innerMethod();        
    }
    class Inner {
            void innerMethod() {
                System.out.println("inner class method...");
            }
    }   
}

in these two cases there are two separate class files. but one gets a compilation error, and the other is fine. what is the reason for this?

+4
source share
6 answers

Local classes have different coverage rules. From JLS, section 6.3 :

"The scope of a local declaration of a class enclosed in a block (§14.2) is the rest of the directly closing block, including its own class declaration."

In the first example, you call the constructor of the Inner class in front of the scope of this inner class, so it is illegal.

TO illustrate this in your code:

void outerMethod() {
    // ...        
    // ...

    // Beginning of legal usage scope of the local class
    class Inner {
        void innerMethod() {
            System.out.println("inner class method...");
        }
    }
    // ...
    // ...
    // End of legal usage scope of the local class
}
+3

II ,

class Outer {    

    void outerMethod() {
        Inner i = new Inner();
        i.innerMethod();        
    }   --> You have the scope change here in this case
    class Inner {
            void innerMethod() {
                System.out.println("inner class method...");
            }
    }   
}

.

, , , ,

+1

1 , . Inner. :

 class Outer {    

    void outerMethod() {
        Inner i = new Inner();    //compile error : cannot find symbol
        i.innerMethod();

        final int localVar = 1;
        class Inner {
            void innerMethod() {
                System.out.println("inner class method... localVar = " + localVar );
            }
        }       
        Inner i = new Inner();    // No Error
        i.innerMethod();
    }
}

, Inner , , .

+1

, Inner, . :

void outerMethod() {
   class Inner {
       void innerMethod() {
           System.out.println("inner class method...");
       }
   }   

   Inner i = new Inner();
   i.innerMethod();
}
0

. :

class Outer {    

    void outerMethod() {
        class Inner { // Inner class definition 
            void innerMethod() {
                System.out.println("inner class method...");
            }
        }       

        Inner i = new Inner();    // No compilation error 
        i.innerMethod();

    }
}
0

in case 1 - think of the class as a variable declaration (since it exists only in the scope of this method), you are trying to assign the variable "inner" when it does not exist, move the class declaration to the beginning of the method and it will compile.

 class Outer {    

    void outerMethod() {
        class Inner {
            void innerMethod() {
                System.out.println("inner class method...");
            }
        }   
        Inner i = new Inner();
        i.innerMethod();   
    }
}
0
source

Source: https://habr.com/ru/post/1535506/


All Articles