Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static class in java

Tags:

java

static

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

static public class Constants {
like image 562
Trevor Avatar asked Nov 19 '25 21:11

Trevor


1 Answers

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
}
like image 181
naikus Avatar answered Nov 22 '25 11:11

naikus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!