Translate

Saturday, April 4, 2020

Constructor in Java

Rules of Constructor

  1. Class name and constructor name must be same
  2. Constructor is able to take parameters
  3. Constructor is not allowed returnType
  4. If you are not creating any constructor in your program then J.V.M or compiler create a default constructor.  
Types of Constructor
  1. Default constructor
  2. User define constructor

Default Constructor

Output :
This is instance m1 method.

Note : 
This is default constructor which is created by JVM. 


User define constructor

Output : 
This is user define 0 args constructor
This is user define parameterized constructor
This is instance m1 method
This is instance m1 method

Note :
When object is created constructor called automatically. At time of calling constructor then memory for the object is created in the memory. Here when two object is creating then object calling automatically and by the help of this two object we can call instance method. Here we create two constructor, first one is user define 0 argument constructor and second one is user define parameterized constructor.

Examples 

Example 1 :
Output :
        " Error..."
Note :
Here we are creating one constructor with 0 argument. But question arise from student side that J.V.M create default constructor; but point is that, if we create user define parameterized  constructor then J.V.M not create default constructor so that we have to create user define non parameterized constructor. Hence this code give Error but it gives the importance role of J.V.M. Ultimately we have to create one more default constructor or we can say user define 0 argument constructor to run this program.   


Example 2 :



Output :
Employe Name    : Vinod Kumar Mishra
Employe Id      : 738.374
Employe Salary  : 20000
Employe Name    : Vinod Kumar Mishra
Employe Id      : 738.374

Employe Salary  : 20000
Note
This program can assign the values of instance variable in constructor. But the disadvantage of this program is it give game output on creating new object. To finish this problem see next example.

Example 3 :

Output :
111
Note :
In this program by using 'this' keyword we can assign the value of local variable in instance variable. 'this' keyword shows the instance variable. By the help of 'this' variable we can modify the last example. See Next Example......

Example 4 :

Output :
Employe Name    : Ratan Sharma
Employe ID         : 142.334
Employe Salary   : 18000
Employe Name    : Kunal Sngh
Employe ID         : 143.584
Employe Salary   : 20500

Note :
Here we are using using this keyword to assign the instance value.

Example 5 :

Note :
Constructor can be change value of instance variable but when constructor is completed instance variable get their actual value. That's why it can not be 31 as program designed. 

Example 6 :

Output :
This is Constructor = 2 and 2
This is Constructor = 1
This is Constructor = 0

Note :
In this program we calling constructor in different constructor by using this keyword but this keyword must be come first in constructor scope. If we call after some statement then it will give Error.

Constructor in Java

Rules of Constructor Class name and constructor name must be same Constructor is able to take parameters Constructor is not allowed re...