This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Originally posted by Amit Saini: Hi, Think of "static" keyword as "one per class".
That's misleading and not helpful, because the keyword static is actually working overtime, when it is allowed to modify member classes:
Instances of class B have an implicitly defined field T.this of type T that refers to the enclosing object. The println statement in b is short for:
Because of the static keyword, instances of class C do not have an inplicitly defined field T.this, so the same println statement causes the compile-time error message listed above.
But you can have as many instance of C as you like: static does not imply "one per class" -- you can call the newC method multiple times; rather static implies, as the JLS puts it in 8.5.2:
Just as a static method of T has no current instance of T in its body, C also has no current instance of T, nor does it have an lexically enclosed instances.
There is no emoticon for what I am feeling!
Joined: Sep 16, 2005
Originally posted by Manoj KG: I am also wondering why Java didn't allow us to make classes static.
For an eg I believe Math class should be an ideal candidate for this. I don't see any scenario where Math class should be instantiated(Unless we want to call any method of Object from the instance).
First, static has a different meaning for member classes -- see my previous reply. Second, Java already has a way to stop a class from being instantiated by clients, and java.lang.Math is taking advantage of it: make its constructors private.
Joined: Dec 01, 2005
Thanks for the reply Jeff.
Now Let me put the question this way.
If we are never going to instantiate a Math class in a real world scenario, why we need a constructor? Also that means that there is piece of code in Math class that will get never executed?
We don't need a constructor for Math. But in Java all classes have constructors. If we don't provide one ourselves, a public constructor is implicitly created for us. We don't want that for Math; instead we have a private constructor as the standard way of making a class uninstantiable under most circumstances.
"I'm not back." - Bill Harding, Twister
Joined: Aug 27, 2004
Hello Friends, Thanks for your keen intrest in this topic.....but i think question still remains the same.
Originally posted by Shrinivas Mujumdar: Hello Friends, Thanks for your keen intrest in this topic.....but i think question still remains the same.
To get to a satisfactory answer, let's do this. Can you describe here what you understand by the term "static". Based on that, we can narrow in why outer classes can't be static. [ January 09, 2006: Message edited by: Stuart Ash ]
To the Math-like example: it would have been possible to have "static" as a modifier for classes that means all methods must be static. The inventors apparently didn't think that's what "static" meant so they didn't do that. [ January 09, 2006: Message edited by: Stan James ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi