This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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: 59092
180
  • Mark post as helpful
  • send pies
  • 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
  • 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: 59092
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer the question I asked and things will become clearer.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 59092
180
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 59092
180
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Whatever. Here's a tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!