What is the purpose of creating static object in Java?

前端 未结 5 2124
走了就别回头了
走了就别回头了 2021-02-06 10:36
class Demo {
   void show() {
      System.out.println(\"i am in show method of super class\");
   }
}
class Flavor1Demo {

   //  An anonymous class with Demo as base c         


        
5条回答
  •  面向向阳花
    2021-02-06 10:59

    That depends on the context you are in.

    The main(String[]) methods is a static methods.

    To stay simple, that means it doesn't exist in an instance of Flavor1Demo, there is no this here. If you set d as non-static, it will only exist in an instance of Flavor1Demo so it can't be access from the main unless you build a instance first (new Flavor1Demo().d.show();

    But a static variable will be link to the class an not an instance, Meaning you can access it from a static context. In you case, the main method.

提交回复
热议问题