

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to call one constructor from another in Java?
You can call one constructor from another using this().
Example
This is a default constructor This is parameterized constructor
public class Sample { int num; public Sample() { System.out.println("This is default constructor"); num = 30; } public Sample(int value) { this(); System.out.println("This is parameterized constructor"); num = value; } public static void main(String args[]){ Sample s = new Sample(30); } }
Output
This is default constructor This is parameterized constructor
- Related Questions & Answers
- Java Program to Call One Constructor from another
- How can we call one constructor from another in the same class in C#?
- How to call the constructor of a superclass from a constructor in java?
- How to call Private Constructor in Java
- How to call another enum value in an enum's constructor using java?
- Can we call a constructor directly from a method in java?
- Java Program to construct one String from another
- Subtract one BigInteger from another BigInteger in Java
- Divide one BigInteger from another BigInteger in Java
- Subtract one BigDecimal from another BigDecimal in Java
- How to explicitly call base class constructor from child class in C#?
- How can we copy one array from another in Java
- Java Program to divide one BigDecimal from another BigDecimal
- Can we implement one interface from another in java?
- Moving a file from one directory to another using Java
Advertisements