Oop Java

Constructor in Java programming language

Constructor in Java programming language

In this tutorial, we will discuss Constructor in Java programming language

The constructor is similar to a method(the Special type of method) but it is not a method. It is called immediately when an instance of the object is created.

When you define constructor you should follow these criteria

  • The constructor must be of the same name as the class name
  • The constructor doesn’t have a return type -(Methods must have a return type)
  • A Java constructor cannot be abstract, static, final and synchronized.

When we create an object each time using the new keyword, at least one constructor is immediately called. It is called the default constructor

There are three types of constructor

  • Default constructor
  • No-argument constructor
  • Parameterized constructor
Type of constructor Constructor in Java programming language

Default constructor

Typically, default constructor doesn’t have any argument in the argument list and body of the constructor is empty. Every time you create an object using new keyword in Java,  least one constructor is immediately called known as default constructor.

Syntax

Cons_name()
{
}

Example

class Cons{
Cons()
{
//statements
}
}

 

How to work default constructor

 

How to call the constructor

Called the constructor

Example

Default constructor displays the default value

Program 1

class cons
{
public static void main(String args[]){
    default_cons obj=new default_cons();
    obj.display();
}
}

class default_cons{
int x;
String name;
public void display(){
System.out.println("Value of x = "+x);
System.out.println("name of boy = "+name);
}
}

 

When the above code is executed, it produces the following results:

Value of x = 0
Name of boy = null

 

When we create an object, the default constructor is called immediately

Program 2

//create and call default constructor in Java
class student_1{
student_1(){  //create a default constructor
System.out.println("Student database has been created");
}

public static void main(String args[]){//main method
student_1 obj=new student_1();// create object for call default constructor

}
}

When the above code is executed, it produces the following results:

Student database has been created

 

No-argument constructor

A constructor which has no argument in the argument list is known as No-argument constructor. Default constructor and no argument constructor both have the same signature. However, the body of the default constructor is empty.

Example

Program 1

class Cons_Demo{
     Cons_Demo()//argument list is emplty
     {
     System.out.println("This is No-argument constructor");
     }

public static void main(String args[]){
    Cons_Demo obj=new Cons_Demo();
}

When the above code is executed, it produces the following results:

This is No-argument constructor

 

Program 2

class My_Sch1{
My_Sch1(){//creating default constructor
System.out.println("This is my school");
System.out.println("This is default constructor");
}
public static void main (String srgs[]){//main method
My_Sch1 obj=new My_Sch1();//when the object created
                              //Automatically called default constructor
}
}

When the above code is executed, it produces the following results:

This is my school

This is no-argument constructor

 

Parameterized constructor

Constructor created with the argument( parameter) is called as Parameterized constructor

Program 1

class Cons_Demo1{
     Cons_Demo1(int age, String name){//parameter
     System.out.println("my age is :"+age);
     System.out.println("my name is :"+name);
     }

public static void main(String args[]){
    Cons_Demo1 obj=new Cons_Demo1(34,"Jhon");//argument given
}
}

When the above code is executed, it produces the following results:

My age is :34
My name is :Jhon

This is a simple example for the parameterized constructor

In this example, the constructor has two parameters: age and name. We created an object named obj and passed an argument for age and name. Constructor executes using those arguments.

Program 2

class Student_data
{
String stu_name;
int stu_id;
//instance variable

//parameterized constructor with two parameter
Student_data(String name,int id){
//assign local variable to global variable
stu_name=name;
stu_id=id;
}
void display(){
System.out.println("Student name is : "+stu_name+" and id is :"+stu_id);
}
public static void main(String args[]){
Student_data stu=new Student_data("Robbert",45);//argument given
stu.display();
}
}

When the above code is executed, it produces the following results:

Student name is : Robbert and id is : 45

Program 3

public class MySchool{
    
int id;
String name;
//instance variavle

MySchool(int stuId, String stuName ){
this.id=stuId;
this.name=stuName;
}//parameterized constructor with two parameter

void stuInfo(){
    System.out.println("Student id is "+id+" and name is "+name);
}

public static void main(String args[]){
    MySchool obj=new MySchool(345,"Niruja");
    MySchool obj1=new MySchool(45,"Laxmi");
    //create object for class
    obj.stuInfo();
    obj1.stuInfo();// call method
}


}

When the above code is executed, it produces the following results:

Student id is 345 and name is Niruja
Student id is 45 and name is Laxmi

 

There are other Java language post that is similar to this post

Constructor overloading in Java language

 

Suggested for you

The method in Java language

Method overloading in Java language

Method overriding in Java language

Constructor overloading in Java programming
Method overloading in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago