为什么Java中构造函数的名称与类名相同?

为什么Java中构造函数的名称与类名相同?

class MyConstructor{ public MyConstructor() { System.out.println("The constructor name should be same as the class name"); } public static void main(String args[]){ MyConstructor mc = new MyConstructor(); } }

在上面的程序中,构造函数的名称应该与类名相同(MyConstructor)。

输出

The constructor name should be same as the class name登录后复制