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

后端 未结 15 1163
借酒劲吻你
借酒劲吻你 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:25

    Java does not permit to declare a constructor as static. Following are the reasons.

    1. Static means for the same class. i.e, static methods cannot be inherited.

    2. With static, "this" reference (keyword) cannot be used. "this" is always linked to an object. A constructor always belongs to some object.

    3. If a constructor is static, an object of subclass cannot access. If static is allowed with constructor, it is accessible within the class but not by subclass.

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

    Static Belongs to Class, Constructor to Object

    We know that static methods, block or variables belong to the class. Whereas a Constructor belongs to the object and called when we use the new operator to create an instance. Since a constructor is not class property, it makes sense that it’s not allowed to be static.

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

    Constructor is the property of an object while static has nothing to do with object. That's why there is nothing like static constructor. But we have static block to do the similar task as constructor i.e. initialization of fields etc.

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

    Java has [static constructors] static initialization blocks which can be viewed as a "static constructor":

    class Foo {
      static String Bar;
      static {
         // "static constructor"
         Bar = "Hello world!";
      }
    }
    

    In any case, the only method in the main class which must be static is the main method. This is because it is invoked without first creating an instance of the "main class". A common technique, and the one I prefer, is to quickly get out of static context:

    class Main {
       int argCount;
    
       // constructor
       public Main (String[] args) {
         // and back to boring ol' non-static Java
         argCount = args.length;       
       }
    
       void runIt () {
          System.out.println("arg count: " + argCount);
       }
    
       // must be static -- no Main instance created yet
       public static void main (String[] args) {
          Main me = new Main(args);
          me.runIt();
       }
    }
    

    Also, static has nothing to do with "name clashes". A static method (or variable) is simply a method (or variable) that is not associated with a specific instance of a type. I would recommend reading through the Classes and Objects Java Tutorial and the section Understanding Instance and Class Variables.

    Happy coding.

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

    A constructor cannot be static, because in an OO language, the process for creating an object is as follows:

    • allocate the object
    • call the constructor to initialise the newly-allocated object

    Constructors are not used anywhere else (and a type-safe language should enforce this), so it follows that a constructor will always be called in a non-static context.

    If a constructor were static, it would not receive a reference to the newly-allocated object, and thus would not be able to initialise it.

    Thus, a constructor can always be non-static (as it is always called from a non-static context) and must always be non-static (otherwise it would be unable to perform its task).

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

    The purpose of Constructor is to Construct an Object i.e. to initialize class's instance variables either their default values or by their initialized values. non-static Instance variables can't be accessed by static methods . So constructor is not static.

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