1
class OuterClass
{
    static class InnerClassOne
    {
        //Class as a static member
    }

    class InnerClassTwo
    {
        //Class as a non-static member
    }
}


class AnotherClassOne extends OuterClass.InnerClassOne
{

}


class AnotherClassTwo extends OuterClass.InnerClassTwo
{
    public AnotherClassTwo()
    {
        new OuterClass().super();  //accessing super class constructor through OuterClass instance
    }
}

i have theses classes, why when extends from nested class we do not call outer class constructor , but when extends from inner class should call outer constructor through outer object, so what is the difference and why??

  • in short , one is static like ready to eat or other one is non-static , need some cooking to use it, read about static and then inner-static class , it will be totally clear & read http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class – Pavneet_Singh Oct 18 '16 at 15:43

1 Answers1

1

The reason is simple. To access a static property you don't need to create an object i.e. you don't need to instantiate the class.

But if you want to access a non static property you will first need to create an object of that class and then use it.

So here in your case when you want to extend InnerClassTwo(which is an inner class and non static) you will have to associate it with the constructor of the outer class AnotherClassTwo, as it can be thought of as a property of that outer class.

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108