Back
How do I call a constructor from another (within the same class not from a subclass)? And what’s the best way to do it?
The best way to do this is to use “this” which is an explicit constructor invocation. Here's the syntax:
public class Foo{ private int z; public Foo(){ this(1); } public Foo(int z) { this.z = z; }}
public class Foo
{
private int z;
public Foo()
this(1);
}
public Foo(int z)
this.z = z;
30.9k questions
32.9k answers
500 comments
665 users