tags:

views:

141

answers:

4

How do I declare a static class in java? eclipse wants me to remove "static" from the declaration.

static public class Constants {
+1  A: 

If by 'static' you mean 'can have only static members', there's no such thing in Java.

Inner classes (and only them) can be static, but that's a different concept. Inner static classes can still have instance members.

Nikita Rybak
ONLY inner classes can be static.
Thilo
@Thilo, I hope you mean "nested" when you say inner. Because "Inner" has a different meaning in the spec. Only "nested" classes can be static. Nested classes that are not static are called Inner classes
naikus
@Nikita, See my comment to Thilo. Inner classes are nested classes that are *not* declared static. So you can't call them inner class if they are declared static :)
naikus
@naikus: +1 Indeed, inner classes require outer instances, and thus are non-static _by definition_. (In other words, inner classes are a subset of nested classes, namely those that are non-static.)
Chris Jester-Young
@naikus, @Chris Thanks for comments about terminology: I just wanted to say that what Trevor wants to do is impossible and din't go far into explaining whole "inner class" concept (which is unrelated to the question anyway).
Nikita Rybak
A: 

All top level classes are implicitly static, meaning they can be accessed by everybody. So it makes sense only to make inner classes optionally static.

fastcodejava
None of this is correct. Static doesn't mean 'can be accessed by everybody', and not all top-level classes can be accessed by everybody. And the conclusion doesn't follow from the premisses.
EJP
+6  A: 

First to answer your question:

Only a Nested class can be declared static. A top level class cannot declared be static.

Secondly, Inner class is a nested class that is not explicitly declared static. See the java language spec. So contrary to some answers here, Inner classes cannot be static

To quote an example from the spec:

class HasStatic{
    static int j = 100;
}
class Outer{
    class Inner extends HasStatic{
        static final int x = 3; // ok - compile-time constant
        static int y = 4; // compile-time error, an inner class
    }
    static class NestedButNotInner{
        static int z = 5; // ok, not an inner class
    }
    interface NeverInner{}  // interfaces are never inner
}
naikus
+1 for not confusing inner classes with nested classes. Huge peeve of mine. :-)
Chris Jester-Young
If I understand correctly, nested classes can be static or not. Nested interfaces, enums, and annotations are all implicitly static.
emory
@emory: That's right.
Chris Jester-Young
Unless you are talking about Java 1.1, which IIRC used "inner classes" to refer to what we now call nested classes. There is also the point that you can have an inner class inside a static class, in which case there is not outer `this`.
Tom Hawtin - tackline
+1  A: 

Eclipse complains correctly, your code won't compile as Top level class can't be declared as static.

You need to first understand what static class means.

static class :

Top level class can't be declared as static. Only Member and Nested top-level classes can be defined as static.

You declare member classes when you want to use variables and methods of the containing class without explicit delegation. When you declare a member class, you can instantiate that member class only within the context of an object of the outer class in which this member class is declared. If you want to remove this restriction, you declare the member class a static class.When you declare a member class with a static modifier, it becomes a nested top-level class and can be used as a normal top-level class as explained above.

nested top-level class is a member classes with a static modifier. A nested top-level class is just like any other top-level class except that it is declared within another class or interface. Nested top-level classes are typically used as a convenient way to group related classes without creating a new package.

Also check http://stackoverflow.com/questions/3504289/when-should-we-go-for-static-class-variables-and-methods-in-java/3504459#3504459

YoK