Can't find a satisfactory answer anywhere.
Because it doesn't add any meaning. 'static' has a meaning when applied to nested classes. It has no meaning on an outer class. So you can't specify it.
We can declare a class static by using the static keyword. A class can be declared static only if it is a nested class. It does not require any reference of the outer class. The property of the static class is that it does not allows us to access the non-static members of the outer class.
Since static variables belong to a class, we can access them directly using class name. So, we don't need any object reference. We can only declare static variables at the class level. We can access static fields without object initialization.
Java doesn't allow you to create top-level classes as static. You can only make a nested class as static . By doing so, you can use the nested class without having an instance of the outer class.
All top-level classes are, by definition, static.
What the static
boils down to is that an instance of the class can stand on its own. Or, the other way around: a non-static inner class (= instance inner class) cannot exist without an instance of the outer class. Since a top-level class does not have an outer class, it can't be anything but static
.
Because all top-level classes are static, having the static
keyword in a top-level class definition is pointless.
Some code to play around with:
public class Foo { public class Bar { // Non-static innner class } public static class Baz { // Static inner class } } public class Example { public static void main(String[] args) { new Foo(); // this is ok new Foo.Baz(); // this is ok new Foo.Bar(); // does not compile! Foo f = new Foo(); Foo.Bar bar = f.new Bar(); //this works, but don't do this } }
I put the "but don't do this" in there because it's really ugly code design. Instance inner classes should not be visible outside the outer class. They should only be used from within the outer class.
Simply put, a top-level type declaration cannot be static, because the Java Language Specification (JLS) doesn't say that it can be. The JLS says this explicitly about the static
keyword as a modifier of top-level classes:
The modifier
static
pertains only to member classes (§8.5.1), not to top level or local or anonymous classes.
However, the accepted answer - which has many upvotes - says that this is because top-level classes are implicitly static "by definition", so the static
modifier would be unnecessary. That is wrong.
The word "static" appears in the JLS in quite a few places, but never to refer to top-level type declarations. Here is an exhaustive list of things that can be "static":
throws
clause are statically thrown.There are no uses of the word "static" in the JLS to refer to top-level type declarations; so as well as not being explicitly static, they are not (and cannot be) "implicitly" static, by definition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With