From 645bd75e78162a3bc114dca4fe2ad1329fddffdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Stevanovi=C4=87?= Date: Mon, 17 Jun 2019 11:22:08 +0200 Subject: [PATCH] Refer to constructor from current class (#33979) * Refer to constructor from current class Refer to constructor from current class * change Java to java --- guide/english/java/constructors/index.md | 33 +++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/guide/english/java/constructors/index.md b/guide/english/java/constructors/index.md index ed3748f532..9aeeece25a 100644 --- a/guide/english/java/constructors/index.md +++ b/guide/english/java/constructors/index.md @@ -154,9 +154,10 @@ The copy constructor is a constructor which creates an object by initializing it 1. Initialize an object from another of the same type. 2. Copy an object to pass it as an argument to a function. 3. Copy an object to return it from a function. - Here is a program that shows a simple use of copy constructor: -```Java +Here is a program that shows a simple use of copy constructor: +```java + class Complex { private double re, im; @@ -178,7 +179,33 @@ class Complex { } ``` -- [run the full code](https://repl.it/MwnJ) +## Refer to constructor from current class +Constructor can also be invoked from another constructor inside of same class. Invocation of one constructor inside of another constructor is being performed by using this keyword which contains all parameteres which signutare of 1st constructor holds. For more clear explanation example is given below: + +```java +class Car { + String brand; + String model; + int seats; + + // 1st constructor + public Car(String brand, String model) { + this.brand = brand; + this.model = model; + } + + // 2nd constructor + public Car(String brand, String model, int seats) { + this(brand, model); // invoking 1st constructor by passing parameters stated inside of this constructors signature + this.seats = seats; + } + +} +``` + + + +[run the full code](https://repl.it/MwnJ) ## Constructor Chaining A chaining constructor is when a constructor calls another constructor from the same class.It can be used for multiple ways to create one Object which can be useful.