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?
Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
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.
Joined: May 09, 2012
According to my knowledge.,
First of All,
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.
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.
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.
Joined: Oct 13, 2005
So how are you going to access a static top level class by the class name?
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.