1

I know how to make an instance of an inner class. But I want to know why we can not do that in the following way:

class outerclass{

      public static void main(String[] args){
              innerclass in=new innerclass();
      }

      class innerclass{


      }

}

If I do this then I get the following error:

No enclosing instance of type outerclass is accessible. Must qualify the allocation with an enclosing instance of type outerclass (e.g. x.new A() where x is an instance of outerclass).

Why?

Tahseen Adit
  • 184
  • 2
  • 7
  • 22
  • 3
    Well, do you know what an "enclosing instance" is? Do you understand that `innerclass` implicitly has a field of type `outerclass`? What would you expect the value of that field to be in your example? – Jon Skeet Sep 05 '16 at 09:59
  • Hmm... imagine how convenient it would be if there was some sort of global internet search where you can see if a particular error message has ever been discussed. – shmosel Sep 05 '16 at 10:17

4 Answers4

3
class Demo{

    public static void main(String[] args){
            System.out.println(innerclass.a);

    }

    static class innerclass{
            static int a=1;

    }


}

Gives the output 1.

See here while making the inner class as static You can easily access in your outer class,In order to create an instance of the Nested class you must reference it by prefixing it with the Outer class name, like this:

Outer.Nested instance = new Outer.Nested();

Non-static Nested Classes (Inner Classes) Non-static nested classes in Java are also called inner classes. Inner classes are associated with an instance of the enclosing class. Thus, you must first create an instance of the enclosing class to create an instance of an inner class. Here is an example inner class definition:

public class Outer {

  public class Inner {
  }

}

Here is how you create an instance of the Inner class:

Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();

Notice how you put new after the reference to the outer class in order to create an instance of the inner class.

Non-static nested classes (inner classes) have access to the fields of the enclosing class, even if they are declared private. Here is an example of that:

public class Outer {

    private String text = "I am private!";

    public class Inner {

        public void printText() {
            System.out.println(text);
        }
    }
}
khakishoiab
  • 9,673
  • 2
  • 16
  • 22
1

Your innerclass is not static. This means it must have a reference to the outerclass. main is static and has no implicit outerclass.

The simple solution is to make your inner class a static nested class.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

You must either make your inner class static (as already mentioned) or create your inner class from a non-static context, e.g. from a non-static method.

I.e. either this:

class outerclass{
  void myMethod() {
      innerclass in = new innerclass();
  }
  class innerclass{
  }
}

or this

class outerclass{
      public static void main(String[] args){
           innerclass in=new innerclass();
      }
      static class innerclass{
      }
}
Samuel Renold
  • 302
  • 1
  • 8
0

outerclass thats encapsulates innerclass is not instantiated, hence, calling innerclass directly would throw an error since there is no outerclass to attach innerclass.

Therefore as suggested by the previous answers, making innerclass static would resolve the problem, allowing access to the innerclass without instantiation.

There are lot of existing answers with regards to this topic. A quick google brings this up.

Java - No enclosing instance of type Foo is accessible

Community
  • 1
  • 1
Samuel Kok
  • 585
  • 8
  • 16