Constructor

A constructor method is a special kind of method that determines how an object is an initialization when created.

They have the same name as the class and do not have any return type.

When the keyword new is used to create an instance of a class, Java allocated memory for the object, initialization the instance variable and class the constructor method

A class may contain one or more construction that provides for the initialization of an object created from the class


Note :

  •  If there is no constructor in a class, the compiler automatically creates a default constructor.
  • A constructor doesn’t have the return type

Syntax of the constructor:

class Temo {
    Temo() {
        // constructor body
    }
}

See a complete example of calling constructor by creating an object

class Main {
   private int x;

   // constructor
   private Main(){
       System.out.println("Constructor Called");
       x = 5;
   }

   public static void main(String[] args){
       // constructor is called while creating object
       Main obj = new Main();
       System.out.println("Value of x = " + obj.x);
   }
}
Constructor Called
Value of x = 5

There is  3 type of constrictor in java

Types of constructor in java

No-Arg constructor:

Which contractor  don’t require any construction is called  No-Arg constructor

No-Arg constructor look like

private Constructor() {
  
   // body of constructor
}

Example of no-arg constructor

class Main {

   int i;

   // constructor with no Argumnet
   private Main(){
       i = 10;
       System.out.println("Object created and i = " + i);
   }

   public static void main(String[] args) {

       // calling the constructor without any parameter
       Main obj = new Main();
   }
}

output

Object created and i = 10

Default Constructor

Its is not possible to create an object of a class without constructor

So when you do not declare any constructor in class then java compiler automatic declare a default constructor

Which constructor is created by java  compiler is called default constructor

Example

class DefaultConstructor {

    int roll = 170070818017;
    String temp =  "Rahul ";

    public static void main(String[] args) {

        // A default constructor is called
        DefaultConstructor obj = new DefaultConstructor();

        System.out.println("my roll no is  = " + obj.temp);
        System.out.println("my name is = " + obj.roll);
    }
}

output

my roll no is  = 170070818017
my name is  = Rahul

here you notice we don’t create any constructor manually

compiler automatic create a default constructor


Parameterized Constructor

Which constructor takes  parameters to execute is called parameterized constructor

Syntax

private Constructor (arg1, arg2, ..., argn) {
    // constructor body
}

See full-example

class Codeelife {

    int name;

    // constructor accepting single value
    private Vehicle(int name){
        this.wheels = wheels;
        System.out.println("hey! "+name + "welcome to codeelife");
    }

    public static void main(String[] args) {

        // calling the constructor by passing single value
        codeelife print = new codeelife("Rahul");
        
    }
}

output

Hey! Rahul welcome to codeelife


SHARE THIS PAGE





Important Link