aspose file tools*
The moose likes Beginning Java and the fly likes Why outer class can't be static? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why outer class can Watch "Why outer class can New topic
Author

Why outer class can't be static?

Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
Hello Friends,
Can anybody tell me Why outer class can't be static?
e.g.

Kudret Serin
Ranch Hand

Joined: Aug 02, 2005
Posts: 167

why should it be?
Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
What is the significance of not making it static in pure technical terms?
Shrinivas
Amit Saini
Ranch Hand

Joined: Oct 20, 2004
Posts: 280
Hi,
Think of "static" keyword as "one per class".
Keeping this definition in mind, having an outer class static does not make sense.
Hope that helps,
Amit
Manoj Kumkumath
Ranch Hand

Joined: Dec 01, 2005
Posts: 71
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).
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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:

System.out.println(T.this.t);

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!
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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.
Manoj Kumkumath
Ranch Hand

Joined: Dec 01, 2005
Posts: 71
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?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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
Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
Hello Friends,
Thanks for your keen intrest in this topic.....but i think question still remains the same.


Shrinivas
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Originally posted by Shrinivas Mujumdar:
Hello Friends,
Thanks for your keen intrest in this topic.....but i think question still remains the same.


Shrinivas


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 ]

ASCII silly question, Get a silly ANSI.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why outer class can't be static?