0 votes
1 view
in Java by (3.5k points)

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?

1 Answer

0 votes
by (46.1k points)

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;

   }

}

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...