I am new to java. When I was going through language specification I found that static classes cannot be declared, but we can have static inner classes. I am little confused
static
is a relative term.
static
means "independent of the containing instance". So a static field has the same value, independent of the instance of the class. A static inner class is valid for every instance of the parent class.
But what would a static
top level class be "independent of the containing instance" of? There is no containing instance for a top level class. That's why it cannot be static
(or, it always is static
, depending on your point of view - but in either way, no need to specify it).
Implementation wise, a non-static
inner class contains a reference to the containing outer class. Obviously this difference also is not possible for top level classes.