Constructors - Parameterized constructors

Constructor in java is a special type of method that is used to initialize the object.
Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.

Rules for creating java constructor

There are basically two rules defined for the constructor.
  1. Constructor name must be same as its class name
  2. Constructor must have no explicit return type

Types of java constructors

There are two types of constructors:
  1. Default constructor (no-arg constructor)
  2. Parameterized constructor
java constructor

Default Constructor

A constructor is said to be default constructor if and only if it never take any parameters.
If any class does not contain at least one user defined constructor than the system will create a default constructor at the time of compilation it is known as system defined default constructor.

Syntax

class className
{       
..... // Call default constructor
clsname ()
{
Block of statements;  // Initialization
}
.....
}
Note: System defined default constructor is created by java compiler and does not have any statement in the body part. This constructor will be executed every time whenever an object is created if that class does not contain any user defined constructor.

Example of default constructor.

In below example, we are creating the no argument constructor in the Test class. It will be invoked at the time of object creation.

Example

//TestDemo.java
class Test
{
int a, b;
Test ()
{
System.out.println("I am from default Constructor...");
a=10;
b=20;
System.out.println("Value of a: "+a);
System.out.println("Value of b: "+b);
}
};
class TestDemo
{
public static void main(String [] args)
{
Test t1=new Test ();
}
};

Output

Output:
I am from default Constructor...
Value of a: 10
Value of b: 20

parameterized constructor

If any constructor contain list of variable in its signature is known as paremetrized constructor. A parameterized constructor is one which takes some parameters.

Syntax

class ClassName
{
.......
ClassName(list of parameters) //parameterized constructor
{
.......
}
.......
}

Syntax to call parametrized constructor

ClassName  objref=new ClassName(value1, value2,.....);
  OR
new ClassName(value1, value2,.....);  

Example of Parametrized Constructor

class Test
{
int a, b;
Test(int n1, int n2)
{
System.out.println("I am from Parameterized Constructor...");
a=n1;
b=n2;
System.out.println("Value of a = "+a);
System.out.println("Value of b = "+b);
}
};
class TestDemo1
{
public static void main(String k [])
{
Test t1=new Test(10, 20);
}
};

Java Copy Constructor

There is no copy constructor in java. But, we can copy the values of one object to another like copy constructor in C++.
There are many ways to copy the values of one object into another in java. They are:
  • By constructor
  • By assigning the values of one object into another
  • By clone() method of Object class
In this example, we are going to copy the values of one object into another using java constructor.
  1. class Student6{  
  2.     int id;  
  3.     String name;  
  4.     Student6(int i,String n){  
  5.     id = i;  
  6.     name = n;  
  7.     }  
  8.       
  9.     Student6(Student6 s){  
  10.     id = s.id;  
  11.     name =s.name;  
  12.     }  
  13.     void display(){System.out.println(id+" "+name);}  
  14.    
  15.     public static void main(String args[]){  
  16.     Student6 s1 = new Student6(111,"Karan");  
  17.     Student6 s2 = new Student6(s1);  
  18.     s1.display();  
  19.     s2.display();  
  20.    }  
  21. }  
Output:
111 Karan
111 Karan

Copying values without constructor

We can copy the values of one object into another by assigning the objects values to another object. In this case, there is no need to create the constructor.
  1. class Student7{  
  2.     int id;  
  3.     String name;  
  4.     Student7(int i,String n){  
  5.     id = i;  
  6.     name = n;  
  7.     }  
  8.     Student7(){}  
  9.     void display(){System.out.println(id+" "+name);}  
  10.    
  11.     public static void main(String args[]){  
  12.     Student7 s1 = new Student7(111,"Karan");  
  13.     Student7 s2 = new Student7();  
  14.     s2.id=s1.id;  
  15.     s2.name=s1.name;  
  16.     s1.display();  
  17.     s2.display();  
  18.    }  
  19. }  
Output:
111 Karan
111 Karan

Difference between Method and Constructor

MethodConstructor
1Method can be any user defined nameConstructor must be class name
2Method should have return typeIt should not have any return type (even void)
3Method should be called explicitly either with object reference or class referenceIt will be called automatically whenever object is created
4Method is not provided by compiler in any case.The java compiler provides a default constructor if we do not have any constructor.

No comments:

Post a Comment