Why not allow the use of Java static constructor?

Java static constructor

Suppose we have a class definition is:

public class Data {    private int id;        public static Data() {}}

If you try to compile this class, you will in the Data type constructor receive an error message, as illegal modifier. Allow only public, protected and private .

Static belong to the class, belonging to the object constructor

We know that static methods, static blocks or variables fall into this category. The constructor belonging to the target, and is called when creating an instance using the new operator. Since the constructor is not a class attribute, it is reasonable to think that it can not be static.

Static block / method can not access non-static variables

We know that static methods can not access non-static variables. Static blocks as well.
Now, the main purpose of a constructor is to initialize the object variable. So, if we set a static constructor, you can not initialize the object variable. This will undermine the whole purpose of creating objects using a constructor function. Therefore, the non-static constructor is reasonable.
Note that we can not use this reference object variable in a static method. The following code defines a compilation error, because: you can not use it in the top of the static .

public static void main(String args []){    System.out.println(this.id);}

Static constructor will inherit destruction

In Java, every class implicitly extends the Object class. We can define a class hierarchy, where the sub-class constructor to call the superclass constructor. This is achieved by super () method call is completed. In most cases, JVM automatically call the superclass constructor, but sometimes if there are multiple superclass constructor, we must call them manually.
Let's look at a super () usage examples.

package com.journaldev.util;class Data {    Data() {        System.out.println("Data Constructor");    }}public class DataChild extends Data{    public DataChild() {        super(); //JRE calls it explicitly, calling here for explanation        System.out.println("DataChild Constructor");    }        public static void main(String args[]) {        DataChild dc = new DataChild();    }}

The above procedure produces the following output.

Date ConstructorDataChild Builder

If you look at the super () method, it is not static. Therefore, if the static constructor conversion, we will not use it, it will destroy the inheritance of java.

 Java static constructor alternative

To initialize the number of static variables in the class, the static blocks may be used. Please note that we can not pass parameters to the static block, so if you want to initialize static variables, you can also do this in conventional constructor.

class Data {    public static int count;        static {        count = 0;    }    Data(int c) {        //not recommended since the count is class variable         //and shared among all the objects of the class        count=c;     }}总结

It explains why we do not allow the use of Java static constructor. We can use a static block and a constructor to initialize static variables themselves
Published 157 original articles · won praise 43 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_39581763/article/details/104197848