aspose file tools*
The moose likes Java in General and the fly likes Why Class cannot be declared as static ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why Class cannot be declared as static ?" Watch "Why Class cannot be declared as static ?" New topic
Author

Why Class cannot be declared as static ?

Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
hi,

Why cannot we declare Class as static ?



why the above code will give me error.

Any object oriented explanation for same.


Thanks
user101
Problems And Solutions - Algorithms
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

Yes there are Static classes- But those have to be Nested classes. More information in the tutorial here.


Mohamed Sanaulla | My Blog
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3396
    
    9
What do you expect the class would do differently if it was static?
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
but if it doesnt behave differently then why such restriction..?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3396
    
    9
The more restrictive a language, the easier it is to use and understand.
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
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

Joined: Aug 16, 2005
Posts: 13884
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3396
    
    9
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

Joined: May 13, 2010
Posts: 104
Thank you very much for an explanation...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why Class cannot be declared as static ?
 
Similar Threads
static method
why top most class cannot be static?
Inner Class cannot have static method ?
anonymous?
static class