What is Constructor?
Constructor is a special type of method whose name is same as class name.
Note:
Syntax
class class_name
{
class_name()
{
// code
}
}
1. Default Constructor: A Constructor which does not have any parameter called default constructor.
Note: It initializes the instance variables to their default values (0 for numeric types, false for boolean, null for objects, etc.)
Syntax
class MyClass {
// Default constructor
MyClass() {
// Initialization code
}
}
Example
class MyClass {
int number;
String text;
boolean bool;
// Default constructor
MyClass() {
// Initialization of instance variables
number = 0;
text = "Default-Constructor";
bool = false;
}
// printing values using disp method
void disp() {
System.out.println(number + " " + text + " " + bool);
}
}
class MyObj {
public static void main(String[] args) {
// Creating an object using the default constructor
MyClass obj = new MyClass ();
obj.disp();
}
}
Output:
0 Default-Constructor false
2. Parameterized Constructor: A constructor that accepts one or more parameters to initialize the instance variables of an object during its creation.
Syntax:
class Car {
// Parameterized constructor
public Car(String make, String model) {
// Initialization code
}
}
Example
class Rectangle {
int length ,width ;
double area;
// Parameterized constructor
Rectangle(int l, int w) {
// Initialize instance variables using constructor parameters
length = l;
width = w;
}
// Method to calculate area
void calculateArea() {
area = length * width;
System.out.println("Length of rectangle: " + area);
}
public static void main(String args[]){
//creating objects and passing values
Rectangle obj= new Rectangle (12,8);
//calling method to display the values of object
obj.calculateArea();
}
}
Output:
Length of rectangle: 96.0
3. Copy Constructor: copy constructor is a special type of constructor that allows you to create a new object by copying the state of an existing object of the same class.
Note: It's used to create a new instance with the same field values as an existing instance.
Syntax:
public class Employee {
int id;
String name;
// copy constructor
public Employee(Employee employee) {
}
}
Example
public class MyClass {
int value;
// Constructor
public MyClass() {
value = 10;
}
// Copy constructor
MyClass(MyClass original) {
value = original.value; // Copy the value from the original object
System.out.println("Value: " + value );
}
public static void main(String[] args) {
MyClass obj1 = new MyClass(); // Creating an object with value 10
MyClass obj2 = new MyClass(obj1); // Using the copy constructor to create obj2 from obj1
}
}
Output:
Value: 10
4. Private Constructor: In java, it is possible to write a constructor as a private but according to the rule we can't access private members outside of class.
Note:
Syntax:
public class PrivateConstructor {
private PrivateConstructor() {
// private constructor
}
}
Example
public class PrivateConstructorExample {
int x;
// Private constructor
private PrivateConstructorExample(int value) {
x = value;
}
// Method within the class to create an instance using the private constructor
public static PrivateConstructorExample createInstance(int value) {
return new PrivateConstructorExample(value);
}
public static void main(String[] args) {
// Accessing the private constructor using a method within the class
PrivateConstructorExample instance = PrivateConstructorExample.createInstance(10);
// Now, you have an instance of the class created using the private constructor
System.out.println("Value: " + instance.x); // Output: Value: 10
}
}
Output:
Value: 10