480

Why are you not able to declare a class as static in Java?

Minar Mahmud
  • 2,577
  • 6
  • 20
  • 32
Mariselvam
  • 4,915
  • 3
  • 20
  • 10
  • 75
    Counter-question: what would you expect the effect to be, if you declared a top-level class to be `static`? – Joachim Sauer Aug 27 '10 at 12:42
  • 3
    No, you can't, except for static inner classes. But, what do you want to achieve with that? – manolowar Aug 27 '10 at 13:00
  • 66
    @Joachim Sauer: Well, C# interprets `static` classes as `abstract final`, i.e., they can't be instantiated and can't be extended. This means they can only contain static members, which is useful for classes that only contain helper methods. – bcat Aug 27 '10 at 13:02
  • 32
    @bcat this is true for C#, but a java class can be either abstract or final, not both. To prevent a class from being instantiated, one can declare a private constructor. – Lorenzo Polidori Feb 06 '12 at 15:08
  • 7
    @JoachimSauer Simple, I'd love to force all the member in the class to be static (maybe with even have to declare on each method or property). – hmartinezd May 22 '14 at 15:52
  • 1
    classes are actually static in the sense that they don't belong to an instance. – njzk2 Aug 29 '14 at 20:46
  • See also: [*Static Classes In Java*](https://stackoverflow.com/q/7486012/642706) – Basil Bourque Feb 18 '19 at 07:03
  • @JoachimSauer and OP, please see my answer. The currently accepted answer is somewhere between deficient and incorrect (see my comment on that answer and my answer itself). – necromancer Apr 06 '19 at 05:48

14 Answers14

495

Only nested classes can be static. By doing so you can use the nested class without having an instance of the outer class.

class OuterClass {
    public static class StaticNestedClass {
    }

    public class InnerClass {
    }

    public InnerClass getAnInnerClass() {
        return new InnerClass();
    }

    //This method doesn't work
    public static InnerClass getAnInnerClassStatically() {
        return new InnerClass();
    }
}

class OtherClass {
    //Use of a static nested class:
    private OuterClass.StaticNestedClass staticNestedClass = new OuterClass.StaticNestedClass();

    //Doesn't work
    private OuterClass.InnerClass innerClass = new OuterClass.InnerClass();

    //Use of an inner class:
    private OuterClass outerclass= new OuterClass();
    private OuterClass.InnerClass innerClass2 = outerclass.getAnInnerClass();
    private OuterClass.InnerClass innerClass3 = outerclass.new InnerClass();
}

Sources :

On the same topic :

Lii
  • 11,553
  • 8
  • 64
  • 88
Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
  • 3
    +1 despite the first phrase not being exact. As the tutorial states "Non-static nested classes are called inner classes." (JLS: "An inner class is a nested class that is not explicitly or implicitly declared static.") – user85421 Aug 27 '10 at 14:18
  • 80
    You're most certainly right; but I don't see how this answers the question – Joeri Hendrickx Aug 27 '10 at 14:18
  • 2
    @Carlos Heuberger, you're right, I updated the post. @Joeri Hendrickx, The question was, "why a class can't be declared as static ..?", they can, and [read my post here]. It's an explanation about the usage of static classes and why they have to be nested-classes. – Colin Hebert Aug 27 '10 at 14:28
  • 1
    The solution given here are fine... But I still wonder y ppl have not explained logically that static class are not possible in java..some OOPs concept explanation would be the right answer. I am awaiting for it – Punith Raj Oct 29 '12 at 19:39
  • 9
    That's true, but the question you're thinking about usually solves itself when you start to ask yourself what a "static class" would be. In java, a static element means that you can access/invoke it without an instance of the enclosing class; what that could possibly mean if you apply the keyword to the class itself? What are your intentions? What would you expect? Now that you have an idea of what it could be, I'm pretty sure that it will be either "not really make sense" or it will be "far fetched" (it would make sense, but it the end it's just a choice that has been made). – Colin Hebert Oct 29 '12 at 21:59
  • The answer, or at least the opening sentence, is wrong: "Only nested classes can be static." See my answer below for the correct, full and brief explanation. The interpretation of this answer that comes closest to being correct but still incomplete is: "Only nested classes can be explicitly marked as static." (@JoeriHendrickx fyi) – necromancer Apr 06 '19 at 05:46
44

Top level classes are static by default. Inner classes are non-static by default. You can change the default for inner classes by explicitly marking them static. Top level classes, by virtue of being top-level, cannot have non-static semantics because there can be no parent class to refer to. Therefore, there is no way to change the default for top-level classes.

necromancer
  • 23,916
  • 22
  • 68
  • 115
  • 15
    »Top level classes are static by default.« This is the best answer. They are static because you don't need any instance of anything to refer to them. They can be referred to from anywhere. They're in *static storage* (as in C storage classes). - As others have noted (Iain Elder in a comment to another answer), the language designers could have allowed the `static` keyword on a top-level class to denote and enforce that it may only contain static members, and not be instantiated; that could, however, have confused people as `static` has a different meaning for nested classes. – Lumi Jan 16 '14 at 10:56
39

So, I'm coming late to the party, but here's my two cents - philosophically adding to Colin Hebert's answer.

At a high level your question deals with the difference between objects and types. While there are many cars (objects), there is only one Car class (type). Declaring something as static means that you are operating in the "type" space. There is only one. The top-level class keyword already defines a type in the "type" space. As a result "public static class Car" is redundant.

Eric Jankowski
  • 463
  • 4
  • 9
  • 1
    Yes. To put it another way, top level classes are static by default, so there's no need for the keyword. – Warren Dew Apr 15 '14 at 03:32
  • 1
    Well, I would add to what Warren Dew said-"top level classes are static by default when accessing static members". – Dexter Aug 23 '14 at 10:26
  • 1
    Underrated answer. A new keyword to force every method in a class to be static and the constructor private would be nice. I wouldn't name it static because that's just confusing. – G_V Dec 15 '14 at 08:46
  • @G_V Although java could create such a monster, Java almost goes out of it's way to make stupid things difficult. Statics are a bad idea in general, static classes are horrible (Worse than singletons which are themselves considered an anti-pattern). Many people don't understand this so giving it a keyword would be a very bad idea. Instead we make bad things difficult and just leave it at that. Note that there is no "Singleton" keyword either. and no Property keyword. These all cause bad code (Although people still use Properties so much that Java might as well support them) – Bill K Aug 08 '17 at 17:24
30

Class with private constructor is static.

Declare your class like this:

public class eOAuth {

    private eOAuth(){}

    public final static int    ECodeOauthInvalidGrant = 0x1;
    public final static int    ECodeOauthUnknown       = 0x10;
    public static GetSomeStuff(){}

}

and you can used without initialization:

if (value == eOAuth.ECodeOauthInvalidGrant)
    eOAuth.GetSomeStuff();
...
Peter Lang
  • 54,264
  • 27
  • 148
  • 161
Sergey Vakulenko
  • 1,655
  • 18
  • 23
  • 3
    It's not static, but contains static properties. If you write public final int ECodeOauthUnknown = 0x10; it's not static anymore. – user1883212 May 07 '13 at 09:40
  • 9
    In case anyone is wondering what is going on here, this matches the C# definition of a "static class" - http://msdn.microsoft.com/en-us/library/79b3xss3%28v=vs.90%29.aspx . Given the Java definition of "static class", all non-inner classes are static (see sibling answers). – Calum Aug 18 '13 at 21:11
  • 1
    Aside from @user1883212 is right, I'd even say that this answer confuses the reader that the private constructor is somehow important here, while it really doesn't matter at all. – tlwhitec Dec 17 '13 at 14:23
  • In the .NET world, if a class is marked both abstract and final (the effect of a `static class` declaration in C#), the compiler won't even allow code to declare variables of that type, nor use it as a generic type parameter. A mere lack of accessible constructors in a class which could otherwise be either instantiated or inherited would not preclude declaration of variables of that type, nor its use as a generic type parameter. – supercat Mar 18 '14 at 21:23
  • This answer is completely incorrect. Private constructors have nothing to do with it. The code is only an example of a class with nothing but static attributes and methods. It has nothing to do with static classes. – user207421 Mar 24 '16 at 04:09
  • What is this? `public static GetSomeStuff(){}` – Asif Mushtaq Nov 30 '16 at 13:15
14

You can create a utility class (which cannot have instances created) by declaring an enum type with no instances. i.e. you are specificly declaring that there are no instances.

public enum MyUtilities {;
   public static void myMethod();
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 26
    an enumeration as a type is a specific expectation: it is an enumerated set of items. Using it for a "utility class", when you could otherwise just have a class with a private constructor, is semantically confusing. – Visionary Software Solutions Jan 26 '13 at 06:21
  • 6
    @VisionarySoftwareSolutions To me, this specificity says there are no instances of this class, rather than indirectly making it impossible to create instances of a class. – Peter Lawrey Jan 26 '13 at 10:59
13

Sure they can, but only inner nested classes. There, it means that instances of the nested class do not require an enclosing instance of the outer class.

But for top-level classes, the language designers couldn't think of anything useful to do with the keyword, so it's not allowed.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • +1 despite the first phrase not being exact. As the JLS states: "An inner class is a nested class that is not explicitly or implicitly declared static." (Just terminology, i know...) – user85421 Aug 27 '10 at 14:21
  • 7
    Although a top-level class can't be declared `static`, it would sometimes make sense to be able to. For example, a class containing only static helper methods makes no sense to be instantiated, but the Java language doesn't stop you from doing it. You can make the class non-instantiable (and practically 'static') to other classes by declaring the default constructor `private`, which forbids instantiation because no constructor is visible. – Iain Samuel McLean Elder Aug 27 '10 at 22:39
9
public class Outer {
   public static class Inner {}
}

... it can be declared static - as long as it is a member class.

From the JLS:

Member classes may be static, in which case they have no access to the instance variables of the surrounding class; or they may be inner classes (§8.1.3).

and here:

The static keyword may modify the declaration of a member type C within the body of a non-inner class T. Its effect is to declare that C is not an inner class. Just as a static method of T has no current instance of T in its body, C also has no current instance of T, nor does it have any lexically enclosing instances.

A static keyword wouldn't make any sense for a top level class, just because a top level class has no enclosing type.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
6

As explained above, a Class cannot be static unless it's a member of another Class.

If you're looking to design a class "of which there cannot be multiple instances", you may want to look into the "Singleton" design pattern.

Beginner Singleton info here.

Caveat:

If you are thinking of using the singleton pattern, resist with all your might. It is one of the easiest DesignPatterns to understand, probably the most popular, and definitely the most abused. (source: JavaRanch as linked above)

S.Jones
  • 1,145
  • 2
  • 12
  • 28
4

In addition to how Java defines static inner classes, there is another definition of static classes as per the C# world [1]. A static class is one that has only static methods (functions) and it is meant to support procedural programming. Such classes aren't really classes in that the user of the class is only interested in the helper functions and not in creating instances of the class. While static classes are supported in C#, no such direct support exists in Java. You can however use enums to mimic C# static classes in Java so that a user can never create instances of a given class (even using reflection) [2]:

public enum StaticClass2 {
    // Empty enum trick to avoid instance creation
    ; // this semi-colon is important

    public static boolean isEmpty(final String s) {
        return s == null || s.isEmpty();
    }
}
shams
  • 3,460
  • 24
  • 24
3

Everything we code in java goes into a class. Whenever we run a class JVM instantiates an object. JVM can create a number of objects, by definition Static means you have the same set of copy to all objects.

So, if Java would have allowed the top class to be static whenever you run a program it creates an Object and keeps overriding on to the same Memory Location.

If You are just replacing the object every time you run it whats the point of creating it?

So that is the reason Java got rid of the static for top-Level Class.

There might be more concrete reasons but this made much logical sense to me.

Yoon5oo
  • 496
  • 5
  • 11
user2626445
  • 1,031
  • 12
  • 27
2

The only classes that can be static are inner classes. The following code works just fine:

public class whatever {
    static class innerclass {
    }
}

The point of static inner classes is that they don't have a reference to the outer class object.

  • 1
    'static inner' is a contradiction in terms. 'static' and 'inner' are mutually exclusive. The word you're looking for is 'nested', not 'inner'. – user207421 Mar 24 '16 at 04:11
1

I think this is possible as easy as drink a glass of coffee!. Just take a look at this. We do not use static keyword explicitly while defining class.

public class StaticClass {

    static private int me = 3;
    public static void printHelloWorld() {
       System.out.println("Hello World");
    }



    public static void main(String[] args) {
        StaticClass.printHelloWorld();
        System.out.println(StaticClass.me);
    }
}

Is not that a definition of static class? We just use a function binded to just a class. Be careful that in this case we can use another class in that nested. Look at this:

class StaticClass1 {

    public static int yum = 4;

    static void  printHowAreYou() {
        System.out.println("How are you?");
    }
}

public class StaticClass {

    static int me = 3; 
    public static void printHelloWorld() {
       System.out.println("Hello World");
       StaticClass1.printHowAreYou();
       System.out.println(StaticClass1.yum);
    }



    public static void main(String[] args) {
        StaticClass.printHelloWorld();
        System.out.println(StaticClass.me);
    }
}
Erfankam
  • 380
  • 5
  • 15
  • I think we can consider such classes as static class by refer to static class definition not by just using static keyword as a qualifier. – Erfankam May 18 '12 at 09:51
  • Be sure to create a private constructor, or else you would be able to say `new StaticClass()` and that doesn't really make much sense. With a private constructor it would not allow instances of it. – grinch Sep 11 '12 at 19:32
1

One can look at PlatformUI in Eclipse for a class with static methods and private constructor with itself being final.

public final class <class name>
{
   //static constants
   //static memebers
}
Himanshu
  • 31,810
  • 31
  • 111
  • 133
user175881
  • 19
  • 1
0

if the benefit of using a static-class was not to instantiate an object and using a method then just declare the class as public and this method as static.

eeadev
  • 3,662
  • 8
  • 47
  • 100