3

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

static public class Constants { 

6 Answers 6

7

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 } 
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for not confusing inner classes with nested classes. Huge peeve of mine. :-)
If I understand correctly, nested classes can be static or not. Nested interfaces, enums, and annotations are all implicitly static.
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.
1

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.

5 Comments

ONLY inner classes can be static.
@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
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: +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.)
@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).
0

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 when should we go for static class,variables and methods in java

Comments

0

As you have already been told from the other comments, classes cannot be declared static. However there are alternatives to this problem.

The most simple one is to precede all member variables and methods with the static modifier. This essentially does what you want.

A slightly more involved alternative is to make the class a singleton. This is a class in which through the use of a private constructor, and an instanceOf() method, or just an Enum, you can only have one instance of that class. Semantically and syntactically you treat that instance as an ordinary instance of whatever particular class you are making a singleton, but you can only have a single instance of that class, which you retrieve via SomeObject.instanceOf(), or in an Enum implementation, SomeObject.INSTANCE.

You would normally use Enums to implement this, minus the edge cases where you are extending another class.

For more complete information on singletons visit the link below.

Design Patterns in Java - Singleton

Comments

0

There is no direct equivalent of C# static classes in Java, but the closest thing in my opinion is an empty enum, which might seem weird at first, but makes sense the more you think about it. An enum in Java (unlike in C#) is essentially a set of singleton instances that all implement the same abstract base class and interfaces. The quickest and safest way to make a normal singleton in Java is like so:

enum Foo { INSTANCE; public Bar doSomething(Baz baz) { return Bar.fromBaz(baz); // yadda yadda } } 

So since we are dealing with sets of singletons, it make sense that we can have an empty set. And an empty set means there can be no instances. This is conceptually the same as a static class in C#.

 enum MyUtilities { ; static Bar doSomething(Baz baz) { return Bar.fromBaz(baz); // yadda yadda } static final String SOME_CONSTANT = "QUX"; } 

This is great because you won't lose test coverage because of hard to test private constructors in a final class, and the code is cleaner than a final class with an empty private constructor.

Now, if the static classes are meant to all work on a single Interface and you have control of that Interface, then you should implement the static methods on that Interface itself (something you can't do in C#).

Comments

-2

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.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.