Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why outer class can't be static?

 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends,
Can anybody tell me Why outer class can't be static?
e.g.

 
Kudret Serin
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why should it be?
 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the significance of not making it static in pure technical terms?
Shrinivas
 
Amit Saini
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends,
Thanks for your keen intrest in this topic.....but i think question still remains the same.


Shrinivas
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic