Default constructor: A constructor that takes no arguments is called a default constructor. If a class does not define any constructors, a default constructor is provided by the compiler. The default constructor initializes all instance variables to their default values (e.g. null for objects, 0 for integers, and false for booleans).
Parameterized constructor: A constructor that takes one or more arguments is called a parameterized constructor. A parameterized constructor is used to initialize instance variables with values passed as arguments during object creation. Parameterized constructors can also call other constructors using the this keyword to reuse code.
Copy constructor: A constructor that takes an object of the same class as a parameter is called a copy constructor. A copy constructor creates a new object with the same state as the parameter object. This is useful when you want to create a new object that is a copy of an existing object.
Here are examples of each type of constructor:
public class Person {
private String name;
private int age;
// Default constructor
public Person() {
name = "";
age = 0;
}
// Parameterized constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Copy constructor
public Person(Person other) {
this.name = other.name;
this.age = other.age;
}
}
1. Default constructor: This constructor is provided by Java if there isn't a constructor explicitly defined in a class. It has no parameters and does not perform any sort of initialization. The main purpose of this constructor is to ensure that an object of the class can be created.
2. Parameterized constructor: This constructor can be defined with one or more parameters, used to initialize the instance variables of the class. When creating an object using a parameterized constructor, the constructor is called with the specified arguments, and the instance variables are initialized with the values passed as arguments.
3. Copy constructor: This constructor is used to create a new object that is a copy of an existing object. However, copy constructors are not provided as default by Java and must be explicitly defined by the programmer.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
There are three types of constructors in Java:
Here are examples of each type of constructor:
Java has the following types of constructors:
1. Default constructor: This constructor is provided by Java if there isn't a constructor explicitly defined in a class. It has no parameters and does not perform any sort of initialization. The main purpose of this constructor is to ensure that an object of the class can be created.
2. Parameterized constructor: This constructor can be defined with one or more parameters, used to initialize the instance variables of the class. When creating an object using a parameterized constructor, the constructor is called with the specified arguments, and the instance variables are initialized with the values passed as arguments.
3. Copy constructor: This constructor is used to create a new object that is a copy of an existing object. However, copy constructors are not provided as default by Java and must be explicitly defined by the programmer.