Illegal modifier error for static class

You can't create a top level static class; that's what the compiler is trying to tell you. Also have a look at the answer here as to why this is the case. The gist is:

What the static boils down to is that an instance of the class can stand on its own. Or, the other way around: a non-static inner class (= instance inner class) cannot exist without an instance of the outer class. Since a top-level class does not have an outer class, it can't be anything but static.

Because all top-level classes are static, having the static keyword in a top-level class definition is pointless.


As the previous answers stated, you can't use the static keyword in top level classes. But i wonder, why did you want it to be static?

Let me show you how a static / non static inner class is used in an example:

public class A
{
    public class B{}

    public static class C{}

    public static void foo()
    {
        B b = new B(); //incorrect

        A a = new A();
        A.B b = a.new B(); //correct

        C c = new C(); //correct
    }
    public void bar()
    {
        B b = new B();
        C c = new C(); // both are correct
    }
}

And from a completely different class:

public class D
{
    public void foo()
    {
        A.B b = new A.B() //incorrect

        A a = new A()
        A.B b = a.new B() //correct

        A.C c = new A.C() //correct
    }
}

1. static canNOT be used at Package level.

2. static is possible within the Class level.

3. But you can still use static on a class, when the class is an inner class, ie. (static inner class), commonly known as Top level class.