Lesson 14
Constructors in Java
A Constructor in Java is a special method that is automatically called when an object is created. Constructors are mainly used to initialize objects and assign values to instance variables.
Lesson content
What is a Constructor in Java?
A constructor is a special member function of a class that initializes objects automatically during object creation.
Constructors have the same name as the class and do not return any value.
Definition List
- Constructor: A special method in Java used to initialize objects automatically when they are created.
- Object Initialization: The process of assigning initial values to object variables.
- Default Constructor: A constructor without parameters.
- Parameterized Constructor: A constructor that accepts parameters to initialize objects with different values.
- Copy Constructor: A constructor that creates a new object using another object of the same class.
Rules of Constructor in Java
A constructor must follow these rules:
- It must have the same name as the class name.
- It cannot have any return type, not even void.
- It is generally declared as public.
Types of Constructors in Java
Java supports the following types of constructors:
- Default Constructor
- Parameterized Constructor
- Copy Constructor
Default Constructor in Java
A default constructor is a constructor without parameters. It automatically initializes objects with default or predefined values.
Example 01: Default Constructor
package Constructor;
class square {
int side, area;
square() {
side = 20;
}
void calculate() {
area = side * side;
}
void display() {
System.out.println("Area of square=" + area);
}
}
class constructor {
public static void main(String args[]) {
square s = new square(); // creating object and constructor is called
s.calculate();
s.display();
}
}Explanation
- The constructor square() initializes the value of side as 20.
- The calculate() method computes the area of the square.
- The display() method prints the result.
Parameterized Constructor in Java
A parameterized constructor accepts parameters to initialize objects with user-defined values.
Example 02: Parameterized Constructor
package Constructor;
class Square_prgm {
int side, area;
Square_prgm(int side) {
this.side = side;
}
void calculate() {
area = side * side;
}
void display() {
System.out.println("Area of square=" + area);
}
}
class parameterized_constructor {
public static void main(String args[]) {
Square_prgm s = new Square_prgm(20); // creating object with parameterized constructor
s.calculate();
s.display();
}
}Explanation
- The constructor accepts the value of side as a parameter.
- this.side = side assigns the parameter value to the instance variable.
- The object is initialized with custom values during creation.
Copy Constructor in Java
A copy constructor creates a new object by copying values from another object of the same class.
Example 03: Copy Constructor
package Constructor;
import java.util.Scanner;
class CopyConstructor {
int age;
String name;
CopyConstructor(int a, String n) {
age = a;
name = n;
}
CopyConstructor(CopyConstructor cc) {
age = cc.age;
name = cc.name;
}
void display() {
System.out.println("Your name is : " + name + "\nAge is : " + age);
}
public static void main(String[] arg) {
System.out.print("Enter your name and age :");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
int age = scan.nextInt();
CopyConstructor cc = new CopyConstructor(age, name);
CopyConstructor c2 = new CopyConstructor(cc);
cc.display();
c2.display();
}
}Explanation of Copy Constructor
Step 1: Create Original Object
CopyConstructor cc = new CopyConstructor(age, name);An object is created using user input values.
Step 2: Create Copy Object
CopyConstructor c2 = new CopyConstructor(cc);The second object copies all values from the first object.
Step 3: Display Values
Both objects display the same data because the values are copied.
Advantages of Constructors in Java
Automatic Initialization
Constructors automatically initialize objects during creation.
Improves Code Reusability
Constructors help create objects with reusable initialization logic.
Supports Multiple Object Creation
Parameterized constructors allow objects to have different values.
Simplifies Object Copying
Copy constructors make it easy to duplicate objects.
Difference Between Methods and Constructors
- Constructor: Used to initialize objects | Method: Used to perform operations
- Constructor: Same name as class | Method: Can have any name
- Constructor: No return type | Method: Can return value
- Constructor: Called automatically | Method: Called explicitly
FAQ
1. What is a constructor in Java?
A constructor is a special method used to initialize objects when they are created.
2. What is the difference between default and parameterized constructors?
A default constructor does not accept parameters, while a parameterized constructor accepts parameters to initialize objects with custom values.
3. What is a copy constructor in Java?
A copy constructor creates a new object by copying data from another object of the same class.
Where Java is used
Java in real-world development
Default Constructor
A constructor without parameters that automatically initializes objects with default or predefined values.
Parameterized Constructor
A constructor that accepts parameters to initialize objects with custom values during creation.
Copy Constructor
A constructor that creates a new object by copying all instance variables from an existing object of the same class.
Constructor Rules
Must have same name as class, no return type, and generally declared as public.
this Keyword
Used in constructors to refer to the current object and distinguish between parameters and instance variables.
Automatic Initialization
Constructors are called automatically when objects are created using the new keyword.