Summary: In this tutorial, you will learn about constructor and constructor overloading in Java with the help of the examples.
Constructor in Java
In Java, a constructor is a special method that is used to initialize an object. It has the same name as the class in which it is defined and is called when an instance of the class is created.
Here is an example of a simple constructor in Java:
class Point {
int x;
int y;
// Constructor
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
In this example, the Point
class has two instance variables: x
and y
. The constructor takes two arguments, x
and y
, and uses them to initialize the instance variables of the same name.
To call the constructor and create an instance of the Point
class, you would use the new
keyword, like this:
Point p = new Point(10, 20);
This would create a new Point
object with x
set to 10 and y
set to 20.
Example with Constructor
Here is an example of a class with a constructor in Java:
class Circle {
double radius;
String color;
// Constructor
Circle(double radius, String color) {
this.radius = radius;
this.color = color;
}
double getArea() {
return Math.PI * radius * radius;
}
public static void main(String[] args) {
Circle c = new Circle(5.0, "red");
double area = c.getArea();
System.out.println("Area of circle: " + area);
}
}
This Circle
class has two instance variables: radius
and color
. The constructor takes two arguments, a double
for the radius and a String
for the color, and uses them to initialize the corresponding instance variables.
The Circle
class also has a method called getArea
that calculates and returns the area of the circle.
When you run this program, it will output the following:
Area of circle: 78.53975
Types of Constructors in Java
There are two types of constructors in Java:
- Default constructors: These are constructors that have no parameters and are automatically provided by the compiler if you don’t write any constructors for your class. Default constructors initialize instance variables to their default values (i.e.,
null
for reference types,0
for numeric types, etc.). - Parameterized constructors: These are constructors that take one or more parameters and are used to initialize the object with specific values.
Here is an example of a class with both a default constructor and a parameterized constructor:
class Point {
int x;
int y;
// Default constructor
Point() {
x = 0;
y = 0;
}
// Parameterized constructor
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
The default constructor can be called with no arguments, like this:
Point p1 = new Point();
This would create a new Point
object with x
and y
both set to 0.
The parameterized constructor can be called with two arguments, like this:
Point p2 = new Point(10, 20);
This would create a new Point
object with x
set to 10 and y
set to 20.
Constructor Overloading in Java
Constructors can also be overloaded, just like regular methods. This means you can have multiple constructors with different parameters in the same class.
This allows you to create multiple ways to initialize an object, depending on the arguments that are passed to the constructor.
For example, consider the following class with two overloaded constructors:
class Point {
int x;
int y;
// Constructor with two int arguments
Point(int x, int y) {
this.x = x;
this.y = y;
}
// Constructor with one int argument
Point(int x) {
this.x = x;
this.y = 0;
}
}
The first constructor takes two int
arguments and initializes the x
and y
instance variables with those values. The second constructor takes only one int
argument and initializes the x
variable with that value, while setting y
to 0.
You can call either constructor depending on the arguments you pass when creating a new object:
Point p1 = new Point(10, 20);
Point p2 = new Point(30);
The first line of code creates a new Point
object with x
set to 10 and y
set to 20. The second line of code creates a new Point
object with x
set to 30 and y
set to 0.
Constructor overloading is a useful feature because it allows you to create objects in different ways depending on your needs. You can have as many overloaded constructors as you need, as long as they have different parameter lists.
To summarize, constructors are used to initialize the state of an object when it is created. They are called automatically when an object is created using the new
keyword, and are used to set any initial values for the object’s instance variables.