I got an error when I try to create new class (that defined in the parent class) from a static method (in the parent class).
error: non-static variable this cannot be referenced from a static context
public class ParentClass {
public class ChildClass {
...
}
public void method () {
// Compiles
ChildClass childClass = new ChildClass();
}
public static void static_method () {
// error: non-static variable this cannot be referenced from a static context
ChildClass childClass = new ChildClass();
}
}
Of course I can creat the ChildClass class as new individual class but I couldn't understand why this wouldn't work.
------- Edit -------
Making the inner class (ChildClass) static would make the class work as separate class (source), as it was in different class, which is exactly what I needed.
public static class ChildClass {
------- Edit #2 -------
Just to make it clear non-static class, as in the first code example, when used and created from non-static method like in method(), can access and use the ParentClass's functions and vriables.