Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 718
Mariselvam Avatar asked Aug 27 '10 12:08

Mariselvam


People also ask

Why can't we declare a class as static in Java?

We can't declare outer (top level) class as static because the static keyword is meant for providing memory and executing logic without creating Objects, a class does not have a value logic directly, so the static keyword is not allowed for outer class.

Can we declare a class as static in Java?

So, Yes, you can declare a class static in Java, provided the class is inside a top-level class. Such clauses are also known as nested classes and they can be declared static, but if you are thinking to make a top-level class static in Java, then it's not allowed.

Why static classes Cannot be instantiated?

It is not possible to create instances of a static class. Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class.

What happens when we declare a class as static in Java?

In Java, static is a keyword that can be used with variables, classes, blocks, and methods. When we use the static keyword before any of them, it means that specified member belongs to a type itself. In other words, an instance of a static member is created and shared across all the instances of the class.


5 Answers

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 :

  • Oracle tutorial on nested classes

On the same topic :

  • Java: Static vs non static inner class
  • Java inner class and static nested class
like image 136
Colin Hebert Avatar answered Oct 19 '22 03:10

Colin Hebert


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.

like image 42
necromancer Avatar answered Oct 19 '22 01:10

necromancer


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.

like image 24
Eric Jankowski Avatar answered Oct 19 '22 03:10

Eric Jankowski


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();
...
like image 31
Sergey Vakulenko Avatar answered Oct 19 '22 03:10

Sergey Vakulenko


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.

like image 13
Michael Borgwardt Avatar answered Oct 19 '22 01:10

Michael Borgwardt