You are on page 1of 12

Constructor

Definition

 A constructor is a special member method of a class that is automatically


called while creating an object to initialize its elements(data member /
attributes).
 It has the same name as the class name.
 It has no return type,not even void.
 Constructors should be declared in the public section of the Class.
 The constructor is called (invoked) automatically when an object of a class
is created.

Rule: If there is no constructor in a class, compiler automatically creates a default constructor.

Rules for creating Java constructor

1. Constructor name must be the same as its class name


2. A Constructor must have no explicit return type
3. A Java constructor cannot be abstract, static, final, and synchronized

Need for constructor:

 Create instance of class.(means creating an object)


 It can be used to set initial values for object attributes(variables) of a
class .

class Test
{

public Test() // Constructor

// body of constructor

Invoking a constructor

 Constructors are invoked automatically when we create objects for the class.

Syntax:

<class name><>obj name> = new <constructor / class name>;

Example:

Test obj= new Test();

Here, Test() will invoke the default constructor.

Why should we define a constructor public.

 By defining public , the objects can be created in any method.


 The constructor can be called outside the class(by creating object)
 Hence the access specifier of a constructor is always public.
Characteristics /Features

 Used to initialize the data members.No arithmatic or logical operations are


performed in a constructor.

 Constructors name must be similar to that of the class name inside which it
resides.This allows the constructors to be invoked automatically on object
creation.)
 Constructors are automatically(implicitly) called when an object is created.

Item ob=new item();

 Constructors cannot be private.


 A constructor cannot be abstract, static, final.
 A constructor can be overloaded.
 Constructors cannot return a value.
 Constructors do not have a return type; not even void.

Reason : Constructors are used to initialize the data members.No arithmatic


or logical operations are performed in a constructor.Hence return from
constructor is not required.

 An abstract class can have the constructor.


 Constructors cannot be inherited; but can be accessed by a subclass
(Reason: instance variables and methods of a class are known as members of a
class. Constructors are not members.For this reason, constructors cannot be
inherited; but can be accessed by a subclass

Types of constructors
a) Parameterised constructor
b) Non Parameterised constructor
c) Default constructor
d) Copy constructor

Parameterised constructor

A parameterised constructor is a member method with same name as the class


name which is used to initialize the instance variables with the help of parametric
values (given as arguments) , passed at the time of creating an object.

Class student

int regno;

String name;

char section;

float average;

student(int x, String y, char z, float f) // Parameterised constructor

regno =x;

name = y;

section=z;

average=f;
}

Void display ()

System.out.println(x);

System.out.println(y);

System.out.println(z);

System.out.println(f);

Public static void main(String args[])

Student stu = new student(5,”Sheela”,’A’,23.4); // parameterised constructor

Stu.display();

}}

// if user did not create object, we have to run the

Program differently to understand that compiler creates its own object( Page 397)

Non Parameterised constructor

A constructor which initialises the instance variables with definite values readily
available within it is called as non – parameterised constructor.

Class student

{
int regno;

String name;

student() // Non – parameterised constructor

regno =10;

name = “Sheela”;

Void display ()

System.out.println(x);

System.out.println(y);

System.out.println(z);

System.out.println(f);

Public static void main(String args[])

Student stu = new student(); // calling non parameterised constructor

Stu.display();

}}

Default constructor:
 The constructor which is used to initialise the data members with default
initial values(0,null,empty values) is called as default constructor.
 We need not explicitly define a default constructor as the compiler will
supply a default constructor.

Class student

int regno;

String name;

char section;

float marks;

Void display ()

System.out.println(regno);

System.out.println(name);

System.out.println(section);

System.out.println(marks);

Public static void main(String args[])

Student stu = new student(); // calling default constructor

Stu.display();
}}

Output:

Null

(empty )

0.0

Note :

 Defining a constructor with arguments hides the default constructor.


 With Parameterised constructor for a class , one must provide initial values
as arguments , otherwise compiler will report an error .

Copy constructor

A constructor that is used to initialize the instance variables of an object by


copying the initial values of the instance variables from another object.

2 Types

a) Direct entry copy constructor


b) Copy constructor by passing object.

Direct entry copy constructor

The initial values of an object is copied by assigning it to another object .

Example:

class copycon
{

int a; // instance variable

copycon(int x) // Parameterised constructor

a=x;

public static void main (String srgs[])

Copycon ob=new copycon(5); // paramerised constructor call

Copycon ob1=ob; // Direct entry copy constructor

Copy constructor by passing object.

Here object is passed to the constructor and the instance variables of current object
are initialised by copying the values from object passed to the constructor.

Example:

class copycon

int a; // instance variable

copycon(int x) // Parameterised constructor

{
a=x;

copycon(copycon ob) //copy constructor by passing object

a=ob.a;

public static void main (String srgs[])

copycon ob=new copycon(5); // paramerised constructor call

copycon ob1=ob; // Direct entry copy constructor

copycon ob1=new copycon(ob) // Copy constructor by passing object

use of constructors

Constructor overloading.

 It is the process of using a number of constructors with the same name but
having different parameter list.
 The compiler differentiates the constructor by taking into account the
number of parameters in the list and their type.
 If a class contains more than one constructors then they are overloading
constructor.
Example:

class copycon

int a; // instance variable

copycon( ) // Non – parameterised constructor

a=10;

copycon(int x) // Parameterised constructor

a=x;

copycon(copycon ob) //Copy constructor by passing object

a=ob.a;

public static void main (String srgs[])

copycon ob=new copycon(); // Non - paramerised constructor call

copycon ob=new copycon(5); // paramerised constructor call

copycon ob1=ob; // Direct entry copy constructor


copycon ob1=new copycon(ob) // Copy constructor by passing object

Difference between constructor and method.

Page 401

You might also like