aspose file tools*
The moose likes Beginning Java and the fly likes Why can't we declare Outer classes or Top level classes as 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 can Watch "Why can New topic
Author

Why can't we declare Outer classes or Top level classes as static

Aashish Vyas
Greenhorn

Joined: May 09, 2012
Posts: 14
Why can't we declare Outer classes or Top level classes as static ?

is there any proper reason ?

thanks..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
Static? What to? Tell us what a top‑level class is static to.
Aashish Vyas
Greenhorn

Joined: May 09, 2012
Posts: 14

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
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
Answer the question I asked and things will become clearer.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8250
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
Winston Gutkowski wrote: . . . Campbell's quite right: . . .
It does happen occasionally
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: May 09, 2012
Posts: 14

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
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
So how are you going to access a static top level class by the class name?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can't we declare Outer classes or Top level classes as static