
每个类对象都是使用相同的new关键字创建的,因此它必须具有关于必须创建对象的类的信息。因此,构造函数的名称应与类名相同。
示例
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











