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 { }
}
}