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 Class cannot be declared as static ?

 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Why cannot we declare Class as static ?



why the above code will give me error.

Any object oriented explanation for same.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes there are Static classes- But those have to be Nested classes. More information in the tutorial here.
 
Stephan van Hulst
Bartender
Pie
Posts: 5790
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you expect the class would do differently if it was static?
 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but if it doesnt behave differently then why such restriction..?
 
Stephan van Hulst
Bartender
Pie
Posts: 5790
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The more restrictive a language, the easier it is to use and understand.
 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what i understand is that there is no effect of declaring class as static as it is same as other class.

But as it is not required that is why class can't be declared as static.

Thank you.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Top-level classes cannot be declared static. It's not useful to be able to declare top-level classes static; what it means to do that is not defined in the Java language specification. So, why would you want to be able to declare a top-level class static, if it doesn't mean anything? The extra word static would only confuse people. So it's a good thing that the Java compiler gives you an error if you try to do this.

Do you mean what static means when it is used on a nested class? Like this:

An instance of a non-static nested class (also called an inner class) exists in the context of an instance of the enclosing class; it can, for example, access the member variables of the instance of the enclosing class:

The difference between an inner class and a static nested class is that the latter exists independent of an instance of the enclosing class - so in a static nested class, you cannot access member variables of the enclosing class.

Top-level classes ofcourse don't have an enclosing class, so you don't need the static modifier for top-level classes to make the distinction.
 
Stephan van Hulst
Bartender
Pie
Posts: 5790
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it's more of a matter of what makes sense. Just think about it. Static means that something is part of a class, not of an instance. An outer class can't be part of an instance or another class, so what sense does it make to say it's static or not?

[Edit]

Jesper beat me to it, and in more detail.
 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for an explanation...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic