- Java Program to Call One Constructor from another
- Example 1: Java program to call one constructor from another
- Example 2: Call the constructor of the superclass from the constructor of the child class
- Java constructor call other constructor
- Constructors. Default constructors. Calling class constructors from other constructors
- Related topics
- Java constructor call other constructor
Java Program to Call One Constructor from another
To understand this example, you should have the knowledge of the following Java programming topics:
Example 1: Java program to call one constructor from another
class Main < int sum; // first constructor Main() < // calling the second constructor this(5, 2); >// second constructor Main(int arg1, int arg2) < // add two value this.sum = arg1 + arg2; >void display() < System.out.println("Sum is: " + sum); >// main class public static void main(String[] args) < // call the first constructor Main obj = new Main(); // call display method obj.display(); >>
In the above example, we have created a class named Main . Here, you have created two constructors inside the Main class.
Main() Main(int arg1, int arg2)
Inside the first constructor, we have used this keyword to call the second constructor.
Here, the second constructor is called from the first constructor by passing arguments 5 and 2.
Note: The line inside a constructor that calls another constructor should be the first line of the constructor. That is, this(5, 2) should be the first line of Main() .
Example 2: Call the constructor of the superclass from the constructor of the child class
We can also call the constructor of the superclass from the constructor of child class using super() .
// superclass class Languages < // constructor of the superclass Languages(int version1, int version2) < if (version1 >version2) < System.out.println("The latest version is: " + version1); >else < System.out.println("The latest version is: " + version2); >> > // child class class Main extends Languages < // constructor of the child class Main() < // calling the constructor of super class super(11, 8); >// main method public static void main(String[] args) < // call the first constructor Main obj = new Main(); >>
In the above example, we have created a superclass named Languages and a subclass Main . Inside the constructor of the Main class, notice the line,
Here, we are calling the constructor of the superclass (i.e. Languages(int version1, int version2) ) from the constructor of the subclass ( Main() ).
Java constructor call other constructor
- Introduction to Java
- The complete History of Java Programming Language
- C++ vs Java vs Python
- How to Download and Install Java for 64 bit machine?
- Setting up the environment in Java
- How to Download and Install Eclipse on Windows?
- JDK in Java
- How JVM Works – JVM Architecture?
- Differences between JDK, JRE and JVM
- Just In Time Compiler
- Difference between JIT and JVM in Java
- Difference between Byte Code and Machine Code
- How is Java platform independent?
- Decision Making in Java (if, if-else, switch, break, continue, jump)
- Java if statement with Examples
- Java if-else
- Java if-else-if ladder with Examples
- Loops in Java
- For Loop in Java
- Java while loop with Examples
- Java do-while loop with Examples
- For-each loop in Java
- Continue Statement in Java
- Break statement in Java
- Usage of Break keyword in Java
- return keyword in Java
- Object Oriented Programming (OOPs) Concept in Java
- Why Java is not a purely Object-Oriented Language?
- Classes and Objects in Java
- Naming Conventions in Java
- Java Methods
- Access Modifiers in Java
- Java Constructors
- Four Main Object Oriented Programming Concepts of Java
- Inheritance in Java
- Abstraction in Java
- Encapsulation in Java
- Polymorphism in Java
- Interfaces in Java
- ‘this’ reference in Java
Constructors. Default constructors. Calling class constructors from other constructors
The default constructor is a constructor without parameters. The default constructor can be declared explicitly in the class or generated automatically. The default constructor can be declared explicitly in the class or generated automatically.
In the most general case, for the ClassName class, the default constructor is as follows:
class ClassName < . // declare the constructor ClassName() < // body of constructor // . > . >
2. In some cases, the default constructor is generated in the class automatically? Example
If the class does not declare any constructor, then the default constructor will be generated. That is, the default constructor is automatically generated in the class only if the class does not contain implementations of other constructors. If the class contains an implementation of at least one constructor with parameters, then to declare the default constructor, it must be explicitly specified in the class.
For example. In the next class declaration, the default constructor is generated automatically
class CMyClass < int d; int GetD() < return d; > void SetD(int nd) < d = nd; > >
The above code means that you can declare a class object using the default constructor:
// It works because the class does not implement any constructors CMyClass mc = new CMyClass();
If you add at least one other constructor to the body of the CMyClass class (for example, a constructor with one parameter), then the default constructor will not automatically be generated
class CMyClass < int d; // declaration of a constructor with 1 parameter, // the default constructor is no longer automatically generated CMyClass(int nd) < d = nd; > int GetD() < return d; > void Set(int nd) < d = nd; > >
After the above implementation, you can not declare an object using the default constructor. However, you can declare an object using a constructor with 1 parameter
// compilation error, because another constructor has already been declared in the class // CMyClass mc = new CMyClass(); CMyClass mc2 = new CMyClass(7); // it works
As a result of the above lines, a compilation error will occurs:
The constructor CMyClass() is undefined
In order to have a default implementation of the constructor and declare a class object using the default constructor, it must be explicitly defined. This can be as follows
class MyClass < int d; // explicit declaration of the default constructor CMyClass() < d = 0; > // declaration of a constructor with 1 parameter CMyClass(int nd) < d = nd; > int GetD() < return d; > void Set(int nd) < d = nd; > >
After such implementation, you can create an instance of the class using two constructors, for example
CMyClass mc = new CMyClass(); // the default constructor is called mc.d = 25; CMyClass mc2 = new CMyClass(5); // the constructor with 1 parameter is called
3. Calling constructors from other constructors. Example
The Java programming language allows you to call the constructors of a class from another constructor of the same class. For this, the ‘this’ keyword is used, which is a reference to the current class.
Example. The example demonstrates the use of the CPixel class, which implements a pixel on the monitor screen.
// A class that implements a pixel on the monitor screen public class CPixel < // internal variables of class private int x, y; // coordinates of pixel private int color; // a color of pixel // constructor without parameters (default constructor) CPixel() < x = y = color = 0; > // constructor with 2 parameters CPixel(int _x, int _y) < x = _x; y = _y; color = 0; > // constructor with 1 parameter CPixel(int _color) < color = _color; x = y = 0; > // constructor with 3 parameters, which calls the constructor with 2 parameters CPixel (int _x, int _y, int _color) < // call the constructor with 2 parameters: only the first operation and only once is called this(_x, _y); //this(_color); // second call of the constructor is forbidden this.color = _color; // this is correct > // access methods int GetX() < return x; > int GetY() < return y; > int GetColor() < return color; > >
Using the CPixel class in another code (method)
. CPixel cp1 = new CPixel(2,8); // calling the constructor with 2 parameters CPixel cp2 = new CPixel(3,5,8); // calling the constructor, which calls another constructore int d; d = cp1.GetX(); // d = 2 d = cp2.GetColor(); // d = 8 d = cp2.GetY(); // d = 5 .
4. What constraints (requirements) are imposed on calling other constructors from the class constructor?
To correctly call other constructors from the class constructor, you must adhere to the following requirements (restrictions):
- only one other class constructor can be called. Call two or more other constructors of this class is prohibited. This follows from the logic that the class constructor is intended to create a class object only once (rather than two or more times);
- calling another constructor must be the first operation in the calling constructor. If the calling constructor calls another constructor to implement the second (third, etc.) operation, the compiler will generate an error.
5. Can be constructor called from the usual class method?
No, it can not be called. A class constructor can only be called from another constructor of the same class. The main purpose of the class constructor is to create instances (objects) of the class. The class methods have not these privilegies.
Related topics
Java constructor call other constructor
- Introduction to Java
- The complete History of Java Programming Language
- C++ vs Java vs Python
- How to Download and Install Java for 64 bit machine?
- Setting up the environment in Java
- How to Download and Install Eclipse on Windows?
- JDK in Java
- How JVM Works – JVM Architecture?
- Differences between JDK, JRE and JVM
- Just In Time Compiler
- Difference between JIT and JVM in Java
- Difference between Byte Code and Machine Code
- How is Java platform independent?
- Decision Making in Java (if, if-else, switch, break, continue, jump)
- Java if statement with Examples
- Java if-else
- Java if-else-if ladder with Examples
- Loops in Java
- For Loop in Java
- Java while loop with Examples
- Java do-while loop with Examples
- For-each loop in Java
- Continue Statement in Java
- Break statement in Java
- Usage of Break keyword in Java
- return keyword in Java
- Object Oriented Programming (OOPs) Concept in Java
- Why Java is not a purely Object-Oriented Language?
- Classes and Objects in Java
- Naming Conventions in Java
- Java Methods
- Access Modifiers in Java
- Java Constructors
- Four Main Object Oriented Programming Concepts of Java
- Inheritance in Java
- Abstraction in Java
- Encapsulation in Java
- Polymorphism in Java
- Interfaces in Java
- ‘this’ reference in Java