-2
class Outer{
    int i = 60;

    class Inner{
        void display(){
            System.out.println(i);
        }
    }

    public static void main(String args[]){
        Inner o = new Inner();
        o.display();
    }
}

Main function does instantiate it's class which are non-static whereas when it comes to instantiating inner class(non-static), java compiler shows up with an error(like the above code). Why?

Edit: I am not asking how to instantiate inner class. I just want a logical reason why main() doesn't instantiate it's inner class while the following function does.

class Outer{
    int i = 60;
    void show(){
        Inner k = new Inner();
        k.display();    
    }

    class Inner{
        void display(){
            System.out.println(i);
        }
    }
    public static void main(String args[]){
        Outer o = new Outer();
        o.show();
    }
}
  • 1
    you should have a reference an instance of the `Outer` to create an `Inner` on it: `new Outer().new Inner()` – Andrew Tobilko Sep 13 '17 at 14:24
  • 2
    *EDIT: I am not asking how to instantiate inner class...* the answers do exactly what your edit does: it creates an instance of the `Outer` class – fantaghirocco Sep 13 '17 at 14:41
  • You can think of nested classes as things similar to methods. You can't use non-static method without an object of class which contains that method. Same with inner classes. To create instance of inner class you need instance of outer class like `Outer ou = new Outer(); Inner in = ou.new Inner();`. Reason for this is that Inner class has access to all members of its outer instance (even private ones) so it must know which outer instance it should be able to access. It happens when you call `ou.new Inner()`, reference to `Outer` instance is saved in created `Inner` instance. – Pshemo Sep 13 '17 at 14:56
  • 1
    Problem is that in `static` context there is no `this`. This is also explained in duplicate question. You probably read only accepted answer, you should take a look also at answers like https://stackoverflow.com/a/12691179 – Pshemo Sep 13 '17 at 15:02

6 Answers6

2

You need an (enclosing) instance of Parent class as child class instance can't exist on it's own, e.g.:

Outer outer = new Outer();
Inner o = outer.new Inner();
o.display();
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
1

Outer class must be instatiated as well:

Inner o = new Outer().new Inner();
fantaghirocco
  • 4,761
  • 6
  • 38
  • 48
1

You need to have a reference of the parent class.

public static void main(String args[]){
    Inner o = new Outer().new Inner();
    o.display();
}
Chinmay jain
  • 979
  • 1
  • 9
  • 20
0

The Java docs says:

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

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

So you can try like:

Inner o = new Outer().new Inner();
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

From Java Tutorials:

Static Nested Classes

As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference.

Inner Classes

As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

So if you want to be able to instantiate an inner class, you need to have an instance of the outer class. In instance methods you don't need it because you're always referring to 'this'

Community
  • 1
  • 1
Luca Negrini
  • 490
  • 4
  • 11
  • Please see the edit and explain. Thankyou – Aayush Sharma Sep 13 '17 at 14:40
  • You can see Inner Classes just as a normal non-static field. They can't be referenced from static context because they are bounded to one specific instance of the class, so when you instantiate them they are visible only by the specific instance of the outer class that you used to create them. Nested Classes instead are like static fields, they are associated with the class object so they can always be created because the class always exists, and they can be accessed by anyone – Luca Negrini Sep 13 '17 at 14:49
  • Another reason why Inner Classes can't be instantiated in a static context is because they have access to non-static fields of the containing class, so making them available from static context could lead to inconsistencies (i.e. NullPointerExceptions, ...) – Luca Negrini Sep 13 '17 at 14:51
0

Because the static methods and attributes of a Class belong to the Class and not at an instance of the Class. For that reason can interact only with other static methods and attributes of the Class.

anemomylos
  • 546
  • 1
  • 6
  • 14