当我们在类中创建任何构造函数(无论是默认构造函数还是带参数构造函数)时,JVM 为什么在编译时不提供构造函数?
最佳答案
以下是 Java 语言规范的摘录:
If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided:
- If the class being declared is the primordial class Object, then the default constructor has an empty body.
- Otherwise, the default constructor takes no parameters and simply invokes the superclass constructor with no arguments.
事实上,只要类不包含构造函数声明,JVM 就会“提供”默认构造函数。与此相同
public MyClass() {}
关于java - 编译时JVM不提供构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3235277/