Why is the Java main method static?

前端 未结 30 2139
离开以前
离开以前 2020-11-22 01:41

The method signature of a Java main() method is:

public static void main(String[] args){
    ...
}

Is the

相关标签:
30条回答
  • 2020-11-22 02:02

    What is the meaning of public static void main(String args[])?

    1. public is an access specifier meaning anyone can access/invoke it such as JVM(Java Virtual Machine.
    2. static allows main() to be called before an object of the class has been created. This is neccesary because main() is called by the JVM before any objects are made. Since it is static it can be directly invoked via the class.

      class demo {    
          private int length;
          private static int breadth;
          void output(){
              length=5;
              System.out.println(length);
          }
      
          static void staticOutput(){
              breadth=10; 
              System.out.println(breadth);
          }
      
          public static  void main(String args[]){
              demo d1=new demo();
              d1.output(); // Note here output() function is not static so here
              // we need to create object
              staticOutput(); // Note here staticOutput() function is  static so here
              // we needn't to create object Similar is the case with main
              /* Although:
              demo.staticOutput();  Works fine
              d1.staticOutput();  Works fine */
          }
      }
      

      Similarly, we use static sometime for user defined methods so that we need not to make objects.

    3. void indicates that the main() method being declared does not return a value.

    4. String[] args specifies the only parameter in the main() method.

      args - a parameter which contains an array of objects of class type String.

    0 讨论(0)
  • 2020-11-22 02:02

    The static key word in the main method is used because there isn't any instantiation that take place in the main method. But object is constructed rather than invocation as a result we use the static key word in the main method. In jvm context memory is created when class loads into it.And all static members are present in that memory. if we make the main static now it will be in memory and can be accessible to jvm (class.main(..)) so we can call the main method with out need of even need for heap been created.

    0 讨论(0)
  • 2020-11-22 02:04

    Before the main method is called, no objects are instantiated. Having the static keyword means the method can be called without creating any objects first.

    0 讨论(0)
  • 2020-11-22 02:04

    Applets, midlets, servlets and beans of various kinds are constructed and then have lifecycle methods called on them. Invoking main is all that is ever done to the main class, so there is no need for a state to be held in an object that is called multiple times. It's quite normal to pin main on another class (although not a great idea), which would get in the way of using the class to create the main object.

    0 讨论(0)
  • 2020-11-22 02:04

    Static methods don't require any object. It runs directly so main runs directly.

    0 讨论(0)
  • 2020-11-22 02:04

    The public static void keywords mean the Java virtual machine (JVM) interpreter can call the program's main method to start the program (public) without creating an instance of the class (static), and the program does not return data to the Java VM interpreter (void) when it ends.

    Source: Essentials, Part 1, Lesson 2: Building Applications

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