• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why outer class can't be static?

 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,
Can anybody tell me Why outer class can't be static?
e.g.

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


Shrinivas
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We should define members as static which

1. Should be common to all objects of the class.
2. Should belong to the class and accessible by class name.
3. 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?

1. 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.
2. 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.
3. 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic