• 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 can't we declare Outer classes or Top level classes as static

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why can't we declare Outer classes or Top level classes as static ?

is there any proper reason ?

thanks..
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static? What to? Tell us what a top‑level class is static to.
 
Aashish Vyas
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Actually, One of the Interviewers asked a question that. we generally specify inner class as static and non static.. but Why we can't declare Outer class as static.? ? ?

as we all know that only inner classes can be declared as static.. not Outer class..

but my question is what is the reason behind that..

thanks in advance..
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer the question I asked and things will become clearer.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aashish Vyas wrote:as we all know that only inner classes can be declared as static.. not Outer class.


Actually, the term "inner class" is generally used to describe a NON-static nested class; so if your interviewer really did use the term "inner" in asking the question, chances are that they either (a) didn't know what they were talking about, or (b) were hoping you would challenge them on the point.

However, Campbell's quite right: Read his question again and try to answer it. What does the keyword static mean?

Winston
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote: . . . Campbell's quite right: . . .

It does happen occasionally
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Static? What to? Tell us what a top‑level class is static to.



@Aashish Vyas: To give you a bit of a hint, can you explain what "static" means in Java? If you know that, it will be obvious why it doesn't make sense for top-level classes to be static.
 
Aashish Vyas
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

According to my knowledge.,


First of All,

1)

When the class is loaded into the memory, At that time all the static variables , static methods and static initialization blocks, if any, are also loaded into memory..

So static members are loaded into memory during loading operation of class.. For that reason, we can't access instance variables from static methods. because they don't know about instances as they are randomly created on the Heap memory.

2)

Static members can be shared by all the objects. So there will not be any separate copy for all the objects. but they all will share a single copy of static variables.

3)

They can be accessed by using the class name, if you try by using instance variable then internally JVM will consider the reference variable of the class.

thanks..
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So how are you going to access a static top level class by the class name?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aashish Vyas wrote:
According to my knowledge.,



Your understanding is more or less correct, but most of what you wrote is not relevant to the question at hand.

What I was getting at is this: In terms of what "static" means to us a Java programmers, it means "this thing is associated with the class in which it's defined, not with any particular instance. The underlined point is the key.



In the first case, "Nested" is associated with the enclosing class "Outer".

In the second case, "StaticOuter" has no enclosing class with which to be associated, so calling it static or non-static is meaningless. Top-level classes don't have the property of staticness/non-staticness.
 
reply
    Bookmark Topic Watch Topic
  • New Topic