Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why can't I use static/private modifiers in outer classes?

 
Woo Hwang
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case of an inner class, what I learnt is that all modifiers except transient can be used, but when I used private or static modifiers in front of ordinary classes, compile error occured. Can someone tell me why?
 
Sam Dalton
Author
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, a private top level class.... can't think of a use for it really, it can NEVER be accessed....
Outer classes are NEVER static, athough their methods may (obviously) be static.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Sam said, a private top level class would be useless.
Something that is static has to be static in relation to something else. A variable or method is static in relation to the class that encloses them. That is to say- the variable or method is valid only at the enclosing class level.
If you try to make the top level class static, what would it be static in relation to? Doesn't make sense.
A nested class can be static in relation to the enclosing class (of course then it is no longer termed an "inner" class).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic