Access Methods Inside Local Inner Classes in Java

Is there a way to access the methods of local inner classes in Java. The following code is an example of the code I tried before. Accordingly, what is the mechanism for accessing the mInner() method?

 class Outer{ int a=100; Object mOuter(){ class Inner{ void mInner(){ int y=200; System.out.println("mInner.."); System.out.println("y : "+y); } } Inner iob=new Inner(); return iob; } } class Demo{ public static void main(String args[]){ Outer t=new Outer(); Object ob=t.mOuter(); ob.mInner(); // ?need a solution.. } } 
+6
source share
3 answers

As the ILikeTau comment says, you cannot access the class that you define in the method. You can define it outside the method, but another possibility is to define an interface (or an abstract class). Then the code will still be inside your method and can access the final variables and parameters defined in the method (which you could not do if you moved the entire class outside). Sort of:

 class Outer { int a = 100; public interface AnInterface { void mInner(); // automatically "public" } AnInterface mOuter() { // note that the return type is no longer Object class Inner implements AnInterface { @Override public void mInner() { // must be public int y = 200; System.out.println("mInner.."); System.out.println("y : " + y); } } Inner iob = new Inner(); return iob; } } class Demo { public static void main(String[] args) { // the preferred syntax Outer t = new Outer(); Outer.AnInterface ob = t.mOuter(); ob.mInner(); } } 

Note: not verified

Note that the return type and ob type have been changed from Object . This is because in Java, if you declare something as an Object , you can only access the methods defined for Object . The compiler should know at compile time (and not at run time) that your ob object has a mInner method, and it cannot say that if the only thing it knows is that it is an Object . Changing it to AnInterface , the compiler now knows that it has a mInner() method.

+5
source

The rules for determining the scope of a local class largely coincide with the rules for determining the scope of a variable, i.e. limited to the closing block.

Similarly, you cannot access the iob variable from main , you cannot access the local Inner class from main .

Outside of the closing block, there is no difference between a local class and an anonymous class. Access to them is also impossible. The difference is that in the closing block access to the local class can be obtained by name, it is especially useful if you need to access it several times, for example. to create multiple instances.

The only way to interact with a local / anonymous class outside the closing block is through any superclass or interface implemented by the class in question.

0
source

To access the inner class, create an object of the inner class.

 OuterClass.InnerClass innerObject = outerObject.new InnerClass(); 

from your example

 outer t=new outer(); outer.inner inner1=t.new inner(); 

Hope this helps you ...

-1
source

All Articles