Having a class itself as its constructor's only parameter

前端 未结 4 1954
星月不相逢
星月不相逢 2021-01-20 09:51
public class Foo{
    private String a;
    private int b;

    public Foo(Foo foo){
        this.a = foo.a;
        this.b = foo.b;
    }
}

Hi eve

4条回答
  •  余生分开走
    2021-01-20 10:17

    You have only 1 constructor, so in order to create an object of class Foo, you need to pass to the constructor a Foo and in order to create that Foo you will need another Foo and it will go on and on . The code can make much more sense if for e.g. you will have a default constructor in the class and then you constructor

    public Foo(Foo foo){
        this.a = foo.a;
        this.b = foo.b;
    }
    

    will act more like a Copy constructor in

提交回复
热议问题