Constructor in Java: Default and Parameterized Constructor with Examples
A constructor is a special method used to initialize the properties of an object. It looks like a method, but it is not. It has no return type, it has the same name as the class, and it runs automatically when you create an object with new. This guide explains the rules, the two types, and the javap output that proves what the compiler actually generates.
Constructor rules in Java
- A constructor is used to initialize the properties of an object.
- A constructor must have the same name as the class.
- A constructor must have an access modifier (public, private, protected, or default).
- A constructor never has non-access modifiers (static, final, abstract, synchronized).
- A constructor is always called with the
newkeyword. - A constructor never has a return type, not even
void. - A constructor always returns an object of the same class.
Types of constructors in Java
There are two types:
- Default constructor (also called no-parameterized constructor)
- Parameterized constructor
1. Default Constructor or Non-Parameterized Constructor
- The compiler provides this constructor when you do not define any constructor in your class.
class A {
}
// save and compile
// then using the javap tool you can see the default constructor like below:
C:\Users\Deep Singh\Desktop>javap A
Compiled from "A.java"
class A {
A(); // this is a default constructor
}
// its access modifier is default
- The default constructor's access modifier depends on the class's access modifier.
public class A {
}
// save and compile
// then using the javap tool you can see the default constructor like below:
C:\Users\Deep Singh\Desktop>javap A
Compiled from "A.java"
public class A {
public A(); // this is a default constructor
}
- A top-level class can never be private or protected. If you want to change the constructor's access modifier to something other than public or default, you must define a no-parameterized constructor yourself.
class A {
// no-parameterized constructor
private A() {
}
}
- If your class contains any user-defined constructor, the compiler never provides the default constructor.
class A {
protected A() {
}
}
// save and compile
// then using the javap tool you can see the output like below:
C:\Users\Deep Singh\Desktop>javap A
Compiled from "A.java"
class A {
protected A(); // there is no default constructor
}
- The default constructor or no-parameterized constructor initializes variables with their default values.
class A {
int i;
public static void main(String[] h) {
A a = new A();
System.out.println(a.i); // 0
}
}
/* If i does not contain any value, its
default value depends on its type.
For int, it is 0. */
Or, with an explicit value:
class A {
int i = 10;
public static void main(String[] h) {
A a = new A();
System.out.println(a.i); // 10
}
}
/* If i contains a value, it is initialized to 10. */
2. Parameterized Constructor
A parameterized constructor is used to take input from the caller to initialize user-specific values.
class A {
int i;
A(int j) {
i = j;
}
public static void main(String[] h) {
A a = new A(20);
System.out.println(a.i); // 20
}
}
Real-world example with a Student class:
class Student {
String name;
int roll;
Student(String n, int r) {
name = n;
roll = r;
}
}
class Test {
public static void main(String[] h) {
Student s = new Student("rahul", 123);
System.out.println(s.name);
System.out.println(s.roll);
}
}
Output is : rahul
Output is : 123
Constructor vs Method: the difference
| Aspect | Constructor | Method |
|---|---|---|
| Name | Same as the class name | Any valid identifier |
| Return type | None, not even void | Must have a return type (or void) |
| Called by | The new keyword |
Explicitly by name |
| Purpose | Initialize an object | Perform an action |
| Inherited | No | Yes |
Where to go next
You now know the two types of constructors, the rules that govern them, and how to inspect what the compiler actually generates using javap.
If you remember one thing, remember this: a constructor has the same name as the class, no return type, and runs when you call new. If you write any constructor yourself, the compiler stops providing the default one.
If you want to go deeper, try this: define a class with a private constructor and see what happens when you try to create an object from another class. Then implement a Singleton using that private constructor. That is the most common real-world use of constructor access control.
No comments:
Post a Comment