-3

We Can’t create inner classes instance inside the outer class static method in java but its working with C#.Please explain the behavior of C# and Java.Any help is very much appreciated.

C# Code:

class OuterClass
{
    public static void Main(string[] args)
    {
        Nested nested = new Nested();
        Nested.InnerNested innerNested = new Nested.InnerNested();
    }
    class Nested
    {
       public class InnerNested
        {
        }
    }
}

Java Code:

public class OuterClass {

    public static void main(String[] args) {

        //Error: non-static variable this cannot be referenced from a static context
        Nested out = new Nested();

        //Error: non-static variable this cannot be referenced from a static context
        Nested.InnerNested i = new Nested.InnerNested();        
    }

    class Nested {

        public class InnerNested {     }

    }

}
yoganathan k
  • 213
  • 4
  • 16
  • Possible duplicate of [C# Nested classes](https://stackoverflow.com/questions/10676003/c-sharp-nested-classes) – Dávid Florek Jun 09 '17 at 12:06
  • 1
    If Java was exactly the same as C# they wouldn't be two languages. Declare your Java inner classes `static`. – khelwood Jun 09 '17 at 12:06
  • 1
    These are two different technologies... dont expect same functionality. – Mehraj Malik Jun 09 '17 at 12:06
  • Ask their creators. Anyway asking why one language does this and another does something dfferent is quite opinion-based and only depends on what the languages creators might have thought when they invented the language. However we don´t know what they thought. – MakePeaceGreatAgain Jun 09 '17 at 12:12
  • I am not C# dev so I can't help you here, but in Java, one of advantages and reasons why we need non-static nested class (inner class) is their ability to access their outer-class instance members (even private ones - which makes them so special since other classes can't do that). Because of that we need to inform inner class about which outer class instance it will be able to access. We do it while construct them like `outerInstance.new Nested()`. In non-static method `outerInstance.` can be represented as `this.` and we don't need to write it, but there is no `this` in static method. – Pshemo Jun 09 '17 at 12:13
  • @HimBromBeere, either that or try to summon Jon Skeet. – Leonardo Pina Jun 09 '17 at 12:15
  • 5
    Possible duplicate of [Java inner class and static nested class](https://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class) – geminiousgoel Jun 09 '17 at 12:27
  • Possible duplicate of [Does C# have an equivalent of Java static nested class?](https://stackoverflow.com/questions/1581977/does-c-sharp-have-an-equivalent-of-java-static-nested-class) – Leonardo Pina Jun 09 '17 at 12:40

1 Answers1

0

Java static nested classes are similar to c# nested classes. Although one can access Inner nested classes in Java by using a reference of an object of the outer class:

Java Code:

public class OuterClass {

    public static void main(String[] args) {

        // Creates a reference of our outer class
        NewClass myClass = new OuterClass();

        // Uses it to create an instance of the nestled class
        OuterClass.Nested nested = OuterClass.new Nested();

        // Uses the nestled class object to instantiate an inner nestled object
        OuterClass.Nested.InnerNested inner = nested.new InnerNested();

    }


    class Nested {

        public class InnerNested {      }

    }
}

This answer was based on the following posts:

Leonardo Pina
  • 458
  • 1
  • 7
  • 17