Constructor in Java Explanation

CONSTRUCTOR


Constructor has name as class name in Java. The Constructor in Java automatically called when the object is created. Constructor doesn’t contain any return value and void. Constructor job is to initialize the internal l state of an object so that the creating an instance will have fully initialized, usable object immediately. It can be used to say initial values for object attributes. All class have constructors by default , if we don’t create a constructor then java create it’s own default constructor.
Constructor classified into two types.
1.Default Constructor
2.Parameterized Constructor

types of constructors in java


DEFAULT CONSTRUCTOR IN JAVA:


The constructor in java doesn’t contain any arguments called as Default Constructor. Let’s begin a program to understand the default constructors.

//Default constructor in Java
class Rectangle {
double length;
double breadth;
Rectangle() //This is the constructor of class Rectangle
{
System.out.println("Constructor called");
length = 20;
breadth = 40;
}
double Area() {
return length*breadth;
}
}
class Demo {
public static void main(String args[]) {
//Declare, initialize and allocate Rectangle objects
Rectangle obj1 = new Rectangle();
Rectangle obj2 = new Rectangle();
double area;
//get Area of first box
area = obj1.Area();
System.out.println("Area of first object is "+area);
//get Area of second object
area = obj2.Area();
System.out.println("Area of second object" +area);
}
}

OUTPUT:

Constructor called
Constructor called
Area of first object is 800.0
Area of second object is 800.0

EXPLANATION OF CONSTRUCTOR IN JAVA:


As you can see above program, both obj1 and obj2 were initialized by Rectangle() Constructor when they were created.

Rectangle obj1 = new Rectangle();

new Rectangle() is calling the Rectangle() Constructor.When you do not explicitly define a constructor for a class, then java creates a default constructor for the class.This line of the program tells how the java creates it’s own default constructor.

PARAMETERIZED CONSTRUCTOR IN JAVA:


Parameterized Constructor overcome the drawbacks of the Default Constructor.If we use Rectangle() constructor it doesn’t initialize different values to different objects.The easy way to vercome the problem is to add parameters to the Constructor.

Default Constructor Program in Java:

//Parametrized construcor program in java                                                             class Rectangle {
double length;
double breadth;
Rectangle(double l,double b) //This is the Parametrized constructor of class Rectangle
{
System.out.println("Constructor called");
length = l;
breadth = b;
}
double Area() {
return length*breadth;
}
}
class Demo {
public static void main(String args[]) {
//Declare, initialize and allocate Rectangle objects
Rectangle obj1 = new Rectangle(20,30);
Rectangle obj2 = new Rectangle(18,30);
double area;
//get Area of first box
area = obj1.Area();
System.out.println("Volume of first object is "+area);
//get Area of second object
area = obj2.Area();
System.out.println("Volume of second object" +area);
}
}

OUTPUT:


Constructor called
Constructor called
Volume of first object is 600.0
Volume of second object540.0

Explanation:
As you can see from the above program each object is intialized with specific parameters.

Rectangle obj1 = new Rectangle(20,30);

The values 20,30 are passed to the Rectangle() constructors when new creates object. Then obj1 copy of length and breadth will contain values of 20,30;

Also read: –> Java syntax Explanation

Summary

Constructor in Java Explanation

Constructor has name as class name in Java. The Constructor in Java automatically called when the object is created. Constructor doesn’t contain any return value and void. Constructor job is to initialize the internal l state of an object so that the creating an instance will have fully initialized, usable object immediately. It can be used to say initial values for object attributes. All class have constructors by default , if we don’t create a constructor then java create it’s own default constructor. Constructor classified into two types. 1.Default Constructor 2.Parameterized Constructor

Leave a comment