Question

Consider:

    package test;
    class Same {
        void method() {
            System.out.println("outer");
        }
    }

    public class Main {
        class Same {
            void method() {
                System.out.println("middle");
            }
        }

    public static void main(String[] args) {
            new Same().method(); // Compile time Exception: No enclosing instance of type Main is accessible
        }
    }

I know that the Inner class 'shadows' the outer class 'Same', thus why I'm getting compile error. I would like to know is it possible to create an instance of 'outer Same' in Main. If yes, how? Thanks.

p.s. I appreciate your answers, but, please first try the code you are suggesting. Also, I know how to change the code to make it work, I need to understand current situation without any change (like keeping the classes in different files).

Was it helpful?

Solution

You need to prefix with the package name (suppose the outer Same is in the test package):

public static void main(String[] args) {
    new test.Same().method(); 
}

This will print "outer".

OTHER TIPS

You need to write

new Main().new Same().method();

In order to instantiate Same class you need an instance of the Main class. Alternative you can declare Same class as a static class

static class Same {...}

Repackage the Top Level Same

package outer;


 public class Same {
        public void method() {
            System.out.println("outer");
        }
    }

Then create an instance using the fully qualified name:

public class Main {
    class Same {
        void method() {
            System.out.println("middle");
        }
    }

    public static void main(String[] args) {
        new outer.Same().method();
    }
}

Or as others have mentioned avoid using the same name for the classes.

A static method cannot refer non static method either you make the non static as static ie.,static class Same {...} or create an object of that non static method and call from static method ie.,new Main().new Same().method();.

package somePackage;

public class SO1 {

    class Same{

    }

    public static void main(String[] args) {
        new somePackage.Same(); //use qualified name
    }
}

class Same{

}

Use the Fully qualified name to outer Same, which is somePackage.Same and that will allow you to refer to outer Same

The error comes because the main method is static, you either need to make Same static or create a new Main() to have Same() inside.

To answer the rest of the question, see this example:

class Test {

    class Same {
        void inner() {

        }
    }

    class Nested {
        class Same {
            void veryInner() {

            }
        }

        void doTest() {
            new Same().veryInner();
            new Test.Same().inner();
        }
    }

     void doTest() {
         new Nested().new Same().veryInner();
         new Same().inner();
     }
}

You can see that the nested shadowing classes do work and that you can access them just by using the right level of nesting.

class Same {
    void method() {
        System.out.println("outer");
    }
}

public class Main {
    static class Same {
        void method() {
            System.out.println("middle");
        }
    }

public static void main(String[] args) {
        new Same().method(); // Compile time Exception: No enclosing instance of type Main is accessible
    }
}

===============================================

main is the static method you must create static class than you create object

Your class is in package test, all you need to do is use

new com.test.Same().method(); 

If your classes are in the default package, ie. no package declaration. Then you can't access the outer Same.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top