Constrocutors

Constructors in Java

A constructor is a block of codes similar to the method. It is called when an instance of the class  is created. At the time of calling constructor, memory for the object is allocated in the memory.

It is a special type of method which is used to initialize the object.

Every time an object is created using the new() keyword, at least one constructor is called.

It calls a default constructor if there is no constructor available in the class. In such case, Java compiler provides a default constructor by default.

In Java constructor is nothing but:

  1. Special function which will have same name of the class.
  2. No need to call constructors as functions
  3. Constructors will be invoked at the time of object creation
  4. At the time of object creation memory will be allocated for the object
  5. Constructors are mainly used for initialization of data
  6. Whenever you create an object with new keywordjfddg it will automatically calls constructor
  7. If there is no constructor in your class java complier will add one default constructor and this will be called by default whenever you create an objectello Java Program for Beginners

There are two types of constructors in Java:

  1. Default constructor (no-arg constructor): If a constructor does not have any parameters then it is known as default constructor. If we don’t have any constructor by default compiler is going to add this default constructor.

 

Syntax for Default constructor

Class_name()

{

code

}

 

 

Example:

//Below program explain about default constructor

class Car{

//creating a default constructor

Car(){

System.out.println(“Car is created”);}

//main method

public static void main(String args[]){

//With below object creation system will execute default constructor.

Car d=new car();

}

}

 

 

  1. Parameterized constructor: If have a constructor with parameters then we are going to call this as parameterized constructor.

Why to use the parameterized constructor?

The parameterized constructor is used to provide different values to objects.

Example of parameterized constructor

In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.

 

//Below program explain about the parameterized constructor.

class Student{

int id;

String name;

// parameterized constructor

Student4(int i,String n){

id = i;

name = n;

}

//method to display the values

void display(){

System.out.println(id+” “+name);}

 

public static void main(String args[]){

//creating objects and passing values

Student s1 = new Student4(1,”Ashok”);

Student s2 = new Student4(2,”Aani”);

//by calling below method to show student details

s1.display();

s2.display();

}

}

Note: We call it as a constructor as it constructs the values at the time of object creation. It is not necessary to write a constructor for a class. It is because java compiler creates a default constructor if your class doesn’t have any.

Constructor Overloading in Java

As mentioned above constructor is nothing but special function so as we overload method we can overload constructor as well. If we have more than one constructor in our program then we call it as a constructor overloading.

Here compiler understand which constructor it should execute based on by the number of parameters in the list and their types.

Example of Constructor Overloading

  • //Below program explains about overloading constructors
  • class Student{
  •     int id;
  •     String name;
  •     int age;
  •     // constructor  with two parameters
  •     Student(int i,String n){
  •     id = i;
  •     name = n;
  •     }
  •     // constructor  with two parameters
  •     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 Student5(1,”Ashok”);
  •     Student s2 = new Student5(222,”Anil”,45);
  •     s1.display();
  •     s2.display();
  •    }
  • }

Difference between constructor and method in Java

Difference between constructor and method in Java

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

Constructor Method
A constructor is mainly used to initialize the data A method is used for specific purpose.
A constructor must not have a return type. A method must have a return type.
The constructor will be automatically called whenever we create an object We need to call methods explicitly
The Java compiler will add one default constructor if you don’t have any constructor in a class. The method is not provided by the compiler in any case.
The constructor name must be same as the class name. The method name may or may not be same as the class name.

Leave a Comment

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

18 Years Experienced Trainer