Java constructor tutorial with easy explanation

What do you mean by constructor ?

A constructor is a special type of method which is used to initialize an object.

Every class contains at least one constructor if we do not create any constructor then compiler 

creates by default constructor.

It does not have any return type.

Object can be created with the ‘new’ keyword

Conditions for the constructor

1.Constructor name must be as class name.

2.Construcor does not contain any return type.

3.Constructor can not be final,abstract,static etc.

Example

class Test
{
String name;
String roll_number;
Test( ) //Here this constructor
{
name=”placement preparation”;
roll_number=”123334”;
}
}

Types of constructor

There are two types of constructor in java

1.default constructor

2.Parameterized constructor

1.default constructor

A constructor is said to be default constructor if it does not have any parameter 

Example:

class Test
{
public Test( )//This is default constructor
{
System.out.println(“Welcome to placementpreparation.com”);
}
Public static void main(String[ ] args)
{
//Here calling the default constructor
Test t1=new Test( );
t1.Test( );
}
}

Output:

Welcome to placementpreparation.com

2.Parameterized constructor

A constructor is said to be parameterized constructor if it does have parameters.

Example

Public class Test
{
Int phone_no;
String name;
Public Test(Int p,String n)
{
phone_no=p;
name=n;
}
Public void display() 
{
System.out.println(phone_no+ “ “+name);
}
Public static void main(String[ ] args)
{
Test t1=new Test(12345,”placement”);
t1.display( );
}
}

Output:

12345 placement

Note:

When constructor is created new object is invoked each time.

Test t1=new Test( ); //This is default constructor

Test t2-new Test(name); //This is parameterized constructor

Constructor overloading

Constructor overloading is same as method overloading but the difference is that method overloading does have return type but constructor overloading does not have any return type.

A constructor is said to be constructor overloading if have multiple constructor contains  different parameter list.

Example of constructor overloading

class Test
{
Int rollNo;
String firstName;
String lastName;
Public Test(int r)Constructor with one argument
{
rollNo=r;
}
Public Test(String f_name,String l_name) //Constructor with two arguments
{
firstName=f_name;
lastName=l_name;
}

Public void display( )
{
System.out.println(rollNo+” ”'+firstName+” “+lastName);
}
Public static void main(String[ ] args)
{
Test t1=new Test(100);
Test t2=new Test(”suman”,”kumar”);
}
}

Output:

100 null null

0 suman kumar

Does constructor return any value ?

Yes,It returns the current instance of the class but remember it return value not data type.

What are the differences between the constructor and normal method

Constructor Normal method

1.It is used to initialise the state of an object & It is invoked while creating object


1.It is used to expose the behaviour of an object


2.It does not contain any return type

2.It does not contain any return type


3.It name must be same as class name


3.It name must be same as class name


4.If there is no constructor,the compiler creates default constructor.

4.If there is no constructor,the compiler creates default constructor.

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *