Constructor in Java

Constructors are more like methods but not method exactly. Constructors are used to assign value for instance variables in any class while creating object.

Even if we did not used any constructor till now, java makes default constructor each time new keyword is used with default values.


Rules for creating constructor

  • Constructor name should be exactly same as class name.
  • It does not have return type but it does have parenthesis and parameters after name like methods.
  • We can have more than one constructors but with different parameters.
  • we cannot use abstract, static or final keywords but we can use access modifiers.


Example

Lets make an simple constructor and see how it works.
Code:

public class LearnConstructor { public LearnConstructor() { System.out.println("I am constructor"); } public static void main(String[] args) { LearnConstructor obj = new LearnConstructor(); } }
Output:
I am constructor
Here we can see that constructor is automatically called when we created an object.


Types of constructor

  • Default Constructor
  • Parameterized Constructor


Default Constructor

A constructor is called "Default Constructor" when it doesn't have any parameter.

Example

Lets see an example where we have constructor in another class.
Code:

public class LearnConstructor { public static void main(String[] args) { ClassTwo obj = new ClassTwo(); obj.Method(); } } class ClassTwo { String name; public ClassTwo() { name = "Empty"; } void Method() { System.out.println("Name : " + name); } }
Output:
Name : Empty
Here, value from default constructor is used in methods.


Parameterized Constructor

A constructor which has a specific number of parameters is called a parameterized constructor. It is used to provide different values to distinct objects.

Example

Lets see another example where we have constructor in another class but with parameters.
Code:

public class LearnConstructor { public static void main(String[] args) { ClassTwo obj = new ClassTwo("BCA", 20); obj.Method(); } } class ClassTwo { String name; int age; public ClassTwo(String s, int x) { name = s; age = x; } void Method() { System.out.println("Name : " + name); System.out.println("Age : " + age); } }
Output:
Name : BCA
Age : 20
Here, value from the object instance used afterwards in methods.


Real Life Example

In a circle either we want area or perimeter, radius is always needed. So, instead of making methods with same parameter multiple times, we can create a constructor and pass value through object directly. And then we dont need parameters with method.

Lets see an example below.
Code:

public class LearnConstructor { public static void main(String[] args) { Circle obj = new Circle(); obj.Area(); obj.Perimeter(); System.out.println(); //Blank Line Circle obj2 = new Circle(5); obj2.Area(); obj2.Perimeter(); } } class Circle { int radius; public Circle() { radius = 0; System.out.println("Radius: " + radius); } public Circle(int x) { radius = x; System.out.println("Radius: " + radius); } void Area() { double a = 3.1416 * radius * radius; System.out.println("Area : " + a); } void Perimeter() { double p = 2 * 3.1416 * radius; System.out.println("Perimeter : " + p); } }
Output:
Radius: 0
Area : 0.0
Perimeter : 0.0

Radius: 5
Area : 78.54
Perimeter : 78.54
Here, we have created two constructors and value is used according to object creation. First constructor is default constructor where value is set to 0. Another constructor is parameterized constructor where value is set by object.


Encapsulation in Java Previous Next this keyword in Java