Why can other methods be “static” but a constructor cannot?

后端 未结 15 1162
借酒劲吻你
借酒劲吻你 2020-12-04 17:59

I do not understand why the main method has to be static. I understand static variables but static methods are difficult for me to grasp. Do static method exists so that one

相关标签:
15条回答
  • 2020-12-04 18:27

    The method declared as static requires no object creation .As we don't create object for the main method it is declared as static.

    constructor is implicitly called to initialize an object, so there is no purpose in having a static constructor.

    0 讨论(0)
  • 2020-12-04 18:37

    The main(String[]) method has a specific prototype that is dictated by how the Java runtime environment works. When you invoke java MyApplication from the command line, the Java VM will look for a static main(String[]) method contained in that class in order to execute the application. If that method is not found, then the Java VM can't run the class as an application. That's just how the language is defined. It also means that the Java VM doesn't create an instance of your application class in order to run it.

    Now, if you want your class to be usable either as a standalone application or as an instance that's created by something else, then you can have your class implement the Runnable interface, and also provide a main method that executes the run method on a new instance.

    public class MyRunnableThing implements Runnable
    {
        // Define whatever variables your runnable thing needs here as
        // private instance fields.
    
        /** Fulfills requirements of Runnable interface. */
        public void run()
        {
            System.out.println( "I'm running..." ) ;
        }
    
        /** Also makes the class runnable from the console. */
        public static void main( String[] args )
        {
            MyRunnableThing runMeNow = new MyRunnableThing() ;
            runMeNow.run() ;
        }
    }
    

    Now any class could potentially create an instance of MyRunnableThing and use its run() method to produce the same behavior that would have been seen by executing java MyRunnablething.

    See also: Working with Static Constructor in Java. Some highlights from that Q&A:

    • A constructor is used to create an instance of the class, so it's an instance method, not a static method.
    • You can create a static method that creates an instance of the class, using the constructor. This is how the trendy new "builder" classes work.
    • You can create a static method that returns a persistent, unique singleton instance.
    • If your class has static members, then you can create a static initializer to initialize the values of those members.
    0 讨论(0)
  • 2020-12-04 18:43

    I wrote a simple example as an answer to a related question yesterday which may help make things more understandable: what's the point of java constructor?

    The point of Static methods is that they can be called without creating an instance of a class, while "normal" instance methods are related to an instance, and can not be called without one.

    Since the Main method of the Main class is the entry point of the program, no instance can possibly have been created yet, so naturally, you can not access it via an instance. Therefore, it is Static, so it can be run as the start of the program.

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