Is it possible to call a constructor from another (within the same class, not from a subclass)? If yes how? And what could be the best way to call another constructor (if there are several ways to do it)?
Java – How to call one constructor from another in Java
constructorjava
Related Topic
- Java – How to round a number to n decimal places in Java
- C++ – call a constructor from another constructor (do constructor chaining) in C++
- Java – How to read / convert an InputStream into a String in Java
- Java – How to generate random integers within a specific range in Java
- Java – How to avoid Java code in JSP files, using JSP 2
- C# – Call one constructor from another
- Java – How to create a memory leak in Java
- Java – What’s the difference between @Component, @Repository & @Service annotations in Spring
Best Answer
Yes, it is possible:
To chain to a particular superclass constructor instead of one in the same class, use
super
instead ofthis
. Note that you can only chain to one constructor, and it has to be the first statement in your constructor body.See also this related question, which is about C# but where the same principles apply.