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

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

Can\'t find a satisfactory answer anywhere.

8条回答
  •  时光取名叫无心
    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.

提交回复
热议问题