What's happening when a members inside a class
is declared as static
..? That members can be accessed without instantiating the class
. Therefore making outer class(top level class) static
has no meaning. Therefore it is not allowed.
But you can set inner classes as static (As it is a member of the top level class). Then that class can be accessed without instantiating the top level class. Consider the following example.
public class A {
public static class B {
}
}
Now, inside a different class C
, class B
can be accessed without making an instance of class A
.
public class C {
A.B ab = new A.B();
}
static
classes can have non-static
members too. Only the class gets static.
But if the static
keyword is removed from class B
, it cannot be accessed directly without making an instance of A
.
public class C {
A a = new A();
A.B ab = a. new B();
}
But we cannot have static
members inside a non-static
inner class.
static class
”? – Weigle