Q 06 - Constructor #9
Answered
by
AmjustGettingStarted
HARSHITH-MV
asked this question in
Q&A
-
Explain in detail constructor and it's types? |
Beta Was this translation helpful? Give feedback.
Answered by
AmjustGettingStarted
Jul 2, 2025
Replies: 1 comment
-
🏗️ Constructors in JavaA constructor in Java is a special method that is called when an object is instantiated. Its main purpose is to initialize the object’s state (i.e., set values to variables). It does not have a return type, not even 🔹 Key Features of Constructors
🔸 Types of Constructors in Java1. Default Constructor
class Car {
Car() {
System.out.println("Default constructor called");
}
} 2. Parameterized Constructor
class Car {
String brand;
Car(String b) {
brand = b;
}
} 3. Copy Constructor
class Car {
String brand;
Car(Car c) {
this.brand = c.brand;
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
HARSHITH-MV
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🏗️ Constructors in Java
A constructor in Java is a special method that is called when an object is instantiated. Its main purpose is to initialize the object’s state (i.e., set values to variables). It does not have a return type, not even
void
, and its name must match the class name.🔹 Key Features of Constructors
abstract
,final
,static
, orsynchronized
.🔸 Types of Constructors in Java
1. Default Constructor