Can't find a satisfactory answer anywhere.
问题:
回答1:
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.
回答2:
I have given the same answer Why can't a Java class be declared as static?. But writing it again here
We should define members as static which
- Should be common to all objects of the class.
- Should belong to the class and accessible by class name.
- Should not need an object of class to access them.
Now suppose we are defining an outer class as static and suppose we are allowed to do so. Will this serve any purpose or provide any advantage to a developer or it will create ambiguity and complications for both developers and language creators?
Let’s check, defining an outer class as static will serve purposes which we have defined above or not?
- Every class is already common to all of its objects and there is no need to make it static to become available to all of its objects.
- We need a class name to access its static members because these members are part of class while an outer class is part of package and we can directly access the class by just writing package_name.class_name (similar to class_name.static_field_name), So again there is no need to do which is already there by default.
- We do not need any object to access a class if it is visible, we can simply write package_name.class_name to access it. And by definition, a class is a blueprint for its objects and we create a class to create objects from it (exception will always be there e.g. java.lang.Math), again there is no need to define an outer class as static.
From above points, we can say Java creators had not allowed an outer class to be static because there is no need to make it static. Allowing to make the outer class static will only increase complications, ambiguity and duplicity. Read more on Why An Outer Java Class Can’t Be Static
回答3:
static
can be added nested classes of an interface, even though this is the default.
I believe static
cannot be added to top level classes because initially there were no nested classes and you couldn't add static to any class.
Later nested class were added and static could be added to nested classes, however there is a tendency not to change the syntax any more than needed so it wasn't added to top level classes. (as there was no need/benefit)
回答4:
Well I guess you dont understand properly if you desire to see a "static" keyword in an outer class.
In short how are you even going to use the feature of static on an outer class?
public class Outer
{
public static int x = 0 ;
}
Now you are going to do Outer.x to access the static variable . This would imply that x shares a single value across all objects of Outer.
Now that we have that away , of what consequence would the static keyword in the Outer class be ? .
回答5:
Whenever we run a class JVM instantiates an object. JVM can create a number of objects, by definition Static means you have same set of copy to all objects.So, if top class is static then whenever you run a program it creates an Object and keeps over riding on to the same Memory Location.
回答6:
- Whenever we run a class JVM will create a object. All static objects are created in static memory and not in heap memory. Meaning, we have same set of copy to all objects. So if the top class is static and u run the pgm, it creates a static object and keep over riding on to the same static memory. which is wrong.
2.We should define members as static which Should be common to all objects of the class. Since, Every class is already common to all of its objects and there is no need to make it static to become available to all of its objects.
回答7:
We can't declare outer (top level) class as static because the static keyword is meant for providing memory and executing logic without creating Objects, a class does not have a value logic directly, so the static keyword is not allowed for outer class.