"The static keyword does not do to a class declaration what it does to a variable or a method declaration."
From: JavaWorld
I understand static
may only be used with nested classes and that makes them equivalent to a top-level class. I also understand that this enables them to declared independently of the enclosing class.
That's the theory, can anybody think of a good practical example as to why we would need to do this? (The example in the link could well have been that of an inner class AFAIK).
Also - the reason behind the question - why have an ambiguous name at all, why "reuse" the word
static
and give it a开发者_JAVA百科 different connotation?
You use it when your class is naturally nested, but has no need for the auto-magic $this parent reference that an inner class has in Java. Every non-static inner class has an embedded reference to its containing instance. When you don't actually need that reference, it's best not to create it. It can cause objects to remain in memory much longer than they need to, and it can also be a pain when you start serializing objects/trying to send them over the network, etc. You can end up with a much larger object graph than you expected or needed to get serialized!
Implementations of Map.Entry are a good example. It's obviously 'natural' for it to be nested in the map implementation, but doesn't have any need to really hold a parent reference back to the map.
For #2, how is it that different? Static on a field or method means it is an attribute of the class definition rather than the instance. You can access and use it without having an instance. Static on a nested class also means that it is an attribute of the class definition, you can access and use it without having an instance.
The visibility of top-level classes may be public or package; but the visibility of a nested static class may be also private or protected. Declaring nested classes may also improve the packaging (structuring) of your classes and relating their dependencies, like
Map.Entry
. Although, it may also be amiguous.The word
static
have the meaning that the instance is declared at the class-level. For example,static int i
declares an integer for a class. A top-level class is the same as declaring a class at class-level is the same.
I can address your second question.
Some other keywords have overloaded meanings, so the static
example is not unique. static
even has another meaning in the context of static imports. It's reusing a word you're already familiar with in another situation where it could make sense, even if it has a totally different meaning.
For example, final
can be used to identify a variable that can be initialized only a single time. It has other meanings, too, like identifying methods that cannot be overridden.
super
and synchronized
are also used in several contexts.
- That's the theory, can anybody think of a good practical example as to why we would >need to do this? (The example in the link could well have been that of an inner class AFAIK).
Doing this assists garbage collecting. Non-static inner classes contain a reference to the outer class.
For instance: If you have a large outer class which creates a Runnable to queue in some task list, the existence of that Runnable may become the only thing that prevents it from being collected. Now the inner class is forcing the JVM to keep around a useless chunk of heap space that it might have to move out of eden and into survivor space which is much harder to collect.
- Also - the reason behind the question - why have an ambiguous name at all, why "reuse" >the word static and give it a different connotation?
What would you prefer to call it? In terms of language, using static doesn't cause any ambiguation in the grammar. To me "static" makes sense. The instance of the inner class is not a member of the outer class instance or vice-versa.
The shared meaning of static
across these contexts is 'resolvable at compile-time' (as opposed to having to look them up on an object instance and go through a virtual method table). This is applicable to static methods, static variables, and static inner classes. I think this is what happens when the compiler writer gets to choose your language keywords, conceptually he groups them all together as 'stuff I can optimize'.
精彩评论