Why can't a top level class be static in Java?

前端 未结 8 1323
礼貌的吻别
礼貌的吻别 2020-12-04 07:30

Can\'t find a satisfactory answer anywhere.

相关标签:
8条回答
  • 2020-12-04 07:54

    Simply put, a top-level type declaration cannot be static, because the Java Language Specification (JLS) doesn't say that it can be. The JLS says this explicitly about the static keyword as a modifier of top-level classes:

    The modifier static pertains only to member classes (§8.5.1), not to top level or local or anonymous classes.

    However, the accepted answer - which has many upvotes - says that this is because top-level classes are implicitly static "by definition", so the static modifier would be unnecessary. That is wrong.

    The word "static" appears in the JLS in quite a few places, but never to refer to top-level type declarations. Here is an exhaustive list of things that can be "static":

    • Static fields, also called static variables, including static constant variables
    • Static methods
    • Static member type declarations
    • "Static members", which are the three constructs above
    • Static initializers
    • Single-static-import declarations and static-import-on-demand declarations, which are top-level declarations, but not type declarations. Here, "static" refers to the names which are imported, not the import declarations themselves.
    • The language is statically typed, and expressions should have statically known types so that their safety is "statically guaranteed".
    • The way that names, including field accesses, are bound at compile-time is referred to as static resolution, or static binding.
    • A lexical context can be a static context.
    • The invocation mode of a method invocation expression or method reference expression can be static.
    • The class name used in one example implies that checked exceptions declared in a throws clause are statically thrown.
    • Part of the memory used by the JVM is referred to as static storage, and the same section refers to "static" linkage in the C programming language.
    • The preface to the JLS mentions static analysis tools.

    There are no uses of the word "static" in the JLS to refer to top-level type declarations; so as well as not being explicitly static, they are not (and cannot be) "implicitly" static, by definition.

    0 讨论(0)
  • 2020-12-04 07:56
    1. Whenever we run a class JVM will create a object. All static objects are created in static memory and not in heap memory. Meaning, we have same set of copy to all objects. So if the top class is static and u run the pgm, it creates a static object and keep over riding on to the same static memory. which is wrong.

    2.We should define members as static which Should be common to all objects of the class. Since, Every class is already common to all of its objects and there is no need to make it static to become available to all of its objects.

    0 讨论(0)
  • 2020-12-04 07:57

    All top-level classes are, by definition, static.

    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.

    Some code to play around with:

    public class Foo {
    
        public class Bar {
             // Non-static innner class
        }
    
        public static class Baz {
             // Static inner class
        }
    }
    
    public class Example {
        public static void main(String[] args) {
            new Foo(); // this is ok
            new Foo.Baz(); // this is ok
            new Foo.Bar(); // does not compile!
    
            Foo f = new Foo();
            Foo.Bar bar = f.new Bar(); //this works, but don't do this
        }
    }
    

    I put the "but don't do this" in there because it's really ugly code design. Instance inner classes should not be visible outside the outer class. They should only be used from within the outer class.

    0 讨论(0)
  • 2020-12-04 08:03

    Whenever we run a class JVM instantiates an object. JVM can create a number of objects, by definition Static means you have same set of copy to all objects.So, if top class is static then whenever you run a program it creates an Object and keeps over riding on to the same Memory Location.

    0 讨论(0)
  • 2020-12-04 08:03

    Well I guess you dont understand properly if you desire to see a "static" keyword in an outer class.

    In short how are you even going to use the feature of static on an outer class?

    public class Outer
    {
       public static int x = 0 ; 
    
    }
    

    Now you are going to do Outer.x to access the static variable . This would imply that x shares a single value across all objects of Outer.

    Now that we have that away , of what consequence would the static keyword in the Outer class be ? .

    0 讨论(0)
  • 2020-12-04 08:03

    The access modifier supported for top level are class are as follows :

    1) public

    2) default

    3) abstract

    4) final

    5) strictfp.

    Reason: Top level class

    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 and mainly as mentioned above static can't be used at Package level. It only used within the Class level.

    0 讨论(0)
提交回复
热议问题