Java Constructor
A constructor in Java is a special method that is used to initialize objects. It is called when an instance of a class is created. Constructors have the same name as the class and do not have a return type. There are two types of constructors in Java:
- Default Constructor (no-arg constructor)
- Parameterized Constructor
##Default Constructor
A default constructor is a no-argument constructor that the Java compiler inserts on your behalf if no constructor is defined in the class.
Example
public class Dog {
String name;
int age;
// Default constructor
public Dog() {
this.name = "Unknown";
this.age = 0;
}
public void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.display();
}
}
Explanation
public Dog()
is the default constructor that initializes thename
andage
attributes.- When
Dog myDog = new Dog();
is executed, the default constructor is called. myDog.display();
prints the default values “Unknown” and 0.
Parameterized Constructor
A parameterized constructor is one that accepts arguments to initialize the object’s attributes with specific values.
Example
public class Dog {
String name;
int age;
// Parameterized constructor
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
public void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
public static void main(String[] args) {
Dog myDog = new Dog("Buddy", 3);
myDog.display();
}
}
Explanation
public Dog(String name, int age)
is the parameterized constructor that initializes thename
andage
attributes with the provided values.- When
Dog myDog = new Dog("Buddy", 3);
is executed, the parameterized constructor is called with “Buddy” and 3 as arguments. myDog.display();
prints “Buddy” and 3.
Constructor Overloading
Constructor overloading is the process of defining multiple constructors in a class, each with a different parameter list.
Example
public class Dog {
String name;
int age;
// Default constructor
public Dog() {
this.name = "Unknown";
this.age = 0;
}
// Parameterized constructor
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
public void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
public static void main(String[] args) {
Dog defaultDog = new Dog();
defaultDog.display();
Dog myDog = new Dog("Buddy", 3);
myDog.display();
}
}
Explanation
- The
Dog
class has both a default constructor and a parameterized constructor. Dog defaultDog = new Dog();
calls the default constructor.Dog myDog = new Dog("Buddy", 3);
calls the parameterized constructor.- Both objects display their respective values when display() is called.
Example Program
Here is a complete example program demonstrating the use of both default and parameterized constructors:
public class Person {
String name;
int age;
// Default constructor
public Person() {
this.name = "Unknown";
this.age = 0;
}
// Parameterized constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
public static void main(String[] args) {
// Using default constructor
Person person1 = new Person();
person1.display();
// Using parameterized constructor
Person person2 = new Person("Alice", 25);
person2.display();
}
}
Explanation
- The
Person
class has both a default constructor and a parameterized constructor. Person person1 = new Person();
calls the default constructor and initializesname
to “Unknown” andage
to 0.Person person2 = new Person("Alice", 25);
calls the parameterized constructor and initializesname
to “Alice” andage
to 25.- Both objects display their respective values when
display()
is called.