- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Java Program to Call One Constructor from another
In this article, we will understand how to call one constructor from another. The keyword 'this()' is used to invoke a constructor.
Below is a demonstration of the same. We will displaying sum and product of two numbers while using this() −
Input
Suppose our input is −
The numbers are defined as 12 and 30
Output
The desired output would be −
The sum is: 42 The product is: 360
Algorithm
Step 1 - START Step 2 - Declare an integer value namely my_sum Step 3 - In the main class, we define a ‘this’ reference to the numbers which would be used as input. Step 4 - This will call the ‘this’ constructor that invokes the current class constructor. Step 5 - Another ‘display’ function is used to display the sum. Step 6 - An object of the class is created, and the functions are invoked to display the result
Example 1
Here, the sum of two numbers is being computed.
public class Main { int my_sum; Main() { this(12, 30); } Main(int my_input_1, int my_input_2) { System.out.println("The numbers are defined as " +my_input_1 +" and " +my_input_2); this.my_sum = my_input_1 + my_input_2; } void display() { System.out.println("The sum is: " + my_sum); } public static void main(String[] args) { Main my_object = new Main(); my_object.display(); } }
Output
The numbers are defined as 12 and 30 The sum is: 42
Example 2
Here, the product of two numbers is being computed.
public class Main { int my_product; Main() { this(12, 30); } Main(int my_input_1, int my_input_2) { System.out.println("The numbers are defined as " +my_input_1 +" and " +my_input_2); this.my_product = my_input_1 * my_input_2; } void display() { System.out.println("The product of the two values is: " + my_product); } public static void main(String[] args) { Main my_object = new Main(); my_object.display(); } }
Output
The numbers are defined as 12 and 30 The product of the two values is: 360
- Related Articles
- How to call one constructor from another in Java?
- 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?
- Java Program to construct one String from another
- How to call another enum value in an enum's constructor using java?
- Java Program to divide one BigDecimal from another BigDecimal
- How to call Private Constructor in Java
- Java Program to copy value from one list to another list
- Can we call a constructor directly from a method in java?
- Java Program to pass method call as arguments to another method
- Can we call a method on "this" keyword from a constructor in java?
- How to write a program to copy characters from one file to another in Java?
- Java Program to copy all the key-value pairs from one Map into another
- Java Program to replace one specific character with another
- How to explicitly call base class constructor from child class in C#?

Advertisements