Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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 (46k 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

Browse Categories

...