Java Constructors
It can be tedious to initialize all of the variables in a class each time an instance is created.
Constructors are an important part of all classes and have many significant attributes. Most real-world classes explicitly define their own constructors within their class definition.
A constructor initializes an object immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to a method. Once defined, the constructor is automatically called immediately after the object is created, before the new operator completes.
Most constructors will not display anything. They will simply initialize an object.
Overloading Constructors:
In addition to overloading normal methods, you can also overload constructor methods.
Types of Constructors
Java constructors can be categorized into several types based on their usage and parameters:
Default Constructors
If you don't define any constructor for a class, Java provides a default constructor (with no parameters) that initializes the object.
For example:
public class MyClass {
// Default constructor (implicitly provided)
}
Non-Parameterized Constructors
A non-parameterized constructor is a constructor that does not accept any parameters.
public class Car {
private String brand;
private String model;
private int year;
// Non-parameterized constructor (default constructor)
public Car() {
this.brand = "Unknown";
this.model = "Unknown";
this.year = 2020; // Default year
}
// Methods to set and get values (omitted for brevity)
}
Parameterized Constructors
These constructors accept parameters, allowing you to initialize an object with specific values. They enable you to customize the initialization process.
public class Employee {
private String name;
private int age;
// Parameterized constructor
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
}
Copy Constructors
A copy constructor accepts an object of the same class and creates a new object by copying the values of the existing object. It helps in creating deep copies of objects.
public class Point {
private int x;
private int y;
// Copy constructor
public Point(Point other) {
this.x = other.x;
this.y = other.y;
}
}
Constructor Overloading
Java supports constructor overloading, where you can define multiple constructors with different parameter lists. This provides flexibility when creating objects.
class Rectangle
{
private int x,y,width,height;
//non param constructor
public Rectangle()
{
x=50;
y=50;
width=100;
height=100;
}
//param constructor
public Rectangle(int w,int h)
{
x=50;
y=50;
width=w; height=h;
}
public Rectangle(int x,int y,int width,int height)
{
this.x=x;
this.y=y;
this.width=width;
this.height=height;
}
//copy constructor
public Rectangle(Rectangle r1)
{
x=r1.x;
y=r1.y;
width=r1.width;
height=r1.height;
}
public void printRectangle()
{
System.out.println("X value is..."+x);
System.out.println("Y Value is.."+y);
System.out.println("Width is..."+width);
System.out.println("Height is..."+height);
System.out.println();
}
}
public class ConstructorDemo2
{
public static void main(String[] args)
{
//invoking non-param constructor
Rectangle r=new Rectangle();
r.printRectangle();
//invoking param constructor
Rectangle r1=new Rectangle(500,200);
r1.printRectangle();
//invoking param constructor
Rectangle r2=new Rectangle(50,75,100,200);
r2.printRectangle();
//invoking copy constructor
Rectangle r3=new Rectangle(r2);
//Rectangle r3=new Rectangle(new Rectangle(500,300));
r3.printRectangle();
}
}