是否可以从另一个(在同一类中,而不是从子类中)调用构造函数?如果是,怎么办?调用另一个构造函数的最佳方法是什么(如果有几种方法)?


当前回答

最初来自米尔科·克莱姆(Mirko Klemm)的回答,为了解决这个问题,稍作修改:

为了完整起见:还有实例初始化块,它总是在调用任何其他构造函数之前执行。它只是由类定义正文中某个位置的语句块“{…}”组成。你甚至可以有不止一个。你不能调用它们,但如果你想在构造函数之间重用一些代码,它们就像“共享构造函数”代码,类似于调用方法。

所以在你的情况下

{ 
  System.out.println("this is shared constructor code executed before the constructor");
  field1 = 3;
}

还有一个“静态”版本用于初始化静态成员:“静态{…}”

其他回答

关键字this可用于从构造函数调用构造函数,当为一个类编写多个构造函数时,有时您希望从另一个构造函数调用一个构造函数以避免重复代码。

Bellow是一个链接,我解释了关于构造函数和getters()和setters()的其他主题,我使用了一个包含两个构造函数的类。我希望这些解释和例子对你有所帮助。

Setter方法或构造函数

可以通过this(…)关键字(当需要从同一类调用构造函数时)或super(…)关键词调用另一个构造函数(当您需要从超类调用构造函数时)。

但是,这样的调用必须是构造函数的第一条语句。要克服这个限制,请使用这个答案。

是的,可以从另一个构造函数调用一个构造函数。但这有一个规则。如果从一个构造函数调用另一个构造函数,那么

该新构造函数调用必须是当前构造函数中的第一条语句

public class Product {
     private int productId;
     private String productName;
     private double productPrice;
     private String category;

    public Product(int id, String name) {
        this(id,name,1.0);
    }

    public Product(int id, String name, double price) {
        this(id,name,price,"DEFAULT");
    }

    public Product(int id,String name,double price, String category){
        this.productId=id;
        this.productName=name;
        this.productPrice=price;
        this.category=category;
    }
}

所以,下面这样的东西是行不通的。

public Product(int id, String name, double price) {
    System.out.println("Calling constructor with price");
    this(id,name,price,"DEFAULT");
}

此外,在继承的情况下,当创建子类的对象时,首先调用超级类构造函数。

public class SuperClass {
    public SuperClass() {
       System.out.println("Inside super class constructor");
    }
}
public class SubClass extends SuperClass {
    public SubClass () {
       //Even if we do not add, Java adds the call to super class's constructor like 
       // super();
       System.out.println("Inside sub class constructor");
    }
}

因此,在这种情况下,在任何其他语句之前,也会先声明另一个构造函数调用。

是的,可以使用this()从另一个构造函数调用一个构造函数

class Example{
   private int a = 1;
   Example(){
        this(5); //here another constructor called based on constructor argument
        System.out.println("number a is "+a);   
   }
   Example(int b){
        System.out.println("number b is "+b);
   }

正如大家已经说过的,您使用this(…),这称为显式构造函数调用。

但是,请记住,在这样的显式构造函数调用语句中,您可能不会引用

任何实例变量或任何实例方法或该类或任何超类中声明的任何内部类,或此或超级的

如JLS(§8.8.7.1)所述。