- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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
Advertisements