14 Constructor

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Constructor in Java

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

1. Java Default Constructor

A constructor that has no parameter is known as default constructor.

Syntax:

<class_name>(){}

Example:

class Bike{

Bike(){System.out.println("Bike is created");}

public static void main(String args[]){

Bike b=new Bike();

1
What is the purpose of default constructor?

Default constructor provides the default values to the object like 0, null etc. depending on the data type.

class Student{

int id;

String name;

void display(){System.out.println(id+" "+name);}

public static void main(String args[]){

Student s1=new Student();

Student s2=new Student();

s1.display();

s2.display();

2. Java parameterized constructor

A constructor that has parameters is known as parameterized constructor. Parameterized constructor is


used to provide different values to the distinct objects.

class Student{

int id;

String name;

Student4(int i,String n){

id = i;

name = n;

void display(){System.out.println(id+" "+name);}

public static void main(String args[]){

Student s1 = new Student(1001,"Ajay");

Student s2 = new Student(1002,"Vijay");

s1.display(); s2.display(); }}

2
Constructor Overloading in Java

Constructor overloading is a technique in Java in which a class can have any number of constructors that
differ in parameter lists. The compiler differentiates these constructors by taking into account the number
of parameters in the list and their type.

class Student{

int id;

String name;

int age;

Student(int i,String n){

id = i;

name = n;

Student(int i,String n,int a){

id = i;

name = n;

age=a;

void display(){System.out.println(id+" "+name+" "+age);}

public static void main(String args[]){

Student s1 = new Student(1001,"Ajay");

Student s2 = new Student(1002,"Vijay",25);

s1.display();

s2.display();

3
Difference between constructor and method in java

There are many differences between constructors and methods. They are given below

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 constructor

class Student{

int id;

String name;

Student(int i,String n){

id = i;

name = n;

Student(Student s){

id = s.id;

name =s.name;

}
4
void display(){System.out.println(id+" "+name);}

public static void main(String args[]){

Student s1 = new Student(1001,"Kunal");

Student s2 = new Student(s1); s1.display(); s2.display(); } }

By assigning the values of one object into another

class Student{

int id;

String name;

Student(int i,String n){

id = i;

name = n;

Student(){}

void display(){System.out.println(id+" "+name);}

public static void main(String args[]){

Student s1 = new Student7(1001,"Kunal");

Student s2 = new Student();

s2.id=s1.id;

s2.name=s1.name;

s1.display();

s2.display();

You might also like