6

Is there any way to access the methods of local inner classes in Java. Following code is the sample code that I tried before. According to that what is the mechanism to access 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..
    }
}
8
  • what is the error that you get
    – Chris Beck
    Sep 5, 2015 at 3:57
  • 1
    You cannot access a class that you define in a method. Define it outside the method and you will be able to access it.
    – saagarjha
    Sep 5, 2015 at 3:58
  • Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method mInner() is undefined for the type Object Sep 5, 2015 at 3:59
  • Is it OK if class Inner is moved to outside the method?
    – saagarjha
    Sep 5, 2015 at 4:05
  • Since Inner does not reference any fields of Outer nor any variables of mOuter(), it would be much better to make the class a static inner class.
    – Andreas
    Sep 5, 2015 at 4:14

3 Answers 3

5

As ILikeTau's comment says, you can't access a class that you define in a method. You could define it outside the method, but another possibility is to define an interface (or abstract class). Then the code would still be inside your method, and could access final variables and parameters defined in the method (which you couldn't do if you moved the whole class outside). Something like:

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 tested

Note that the return type, and the type of ob, have been changed from Object. That's because in Java, if you declare something to be an Object, you can only access the methods defined for Object. The compiler has to know, at compile time (not at run time) that your object ob has an mInner method, and it can't tell that if the only thing it knows is that it's an Object. By changing it to AnInterface, the compiler now knows that it has an mInner() method.

0
0

The scoping rules of a local class are pretty much the same as the scoping rules of a variable, that is, it is confined to the enclosing block.

The same way you cannot access variable iob from main, you cannot access local class Inner from main.

Outside the enclosing block, there's no difference between a local class and an anonymous class. Neither can be accessed. The difference is that within the enclosing block, the local class can be accessed by name, especially useful if you need to access it repeatedly, e.g. to create multiple instances.

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

-1

To access the inner class create an object of 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
  • @JavaLearner Your answer is for suitable to access regular inner classes. It is not applicable for local inner classes. Sep 5, 2015 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.