Java Constructors
- In Java, constructors play an important role in object creation.
- A constructor is a special block of code that is called when an object is created.
- Its main job is to initialize the object, to set up its internal state, or to assign default values to its attributes.
- This process happens automatically when we use the "new" keyword to create an object.
- A constructor in Java is similar to a method that is invoked when an object of the class is created.
Characteristics of Constructors:
- Same Name as the Class: A constructor has the same name as the class in which it is defined.
- No Return Type: Constructors do not have any return type, not even void. The main purpose of a constructor is to initialize the object, not to return a value.
- Automatically Called on Object Creation: When an object of a class is created, the constructor is called automatically to initialize the object’s attributes.
- Used to Set Initial Values for Object Attributes: Constructors are primarily used to set the initial state or values of an object’s attributes when it is created.
Syntax
class ClassName {
ClassName() {
}
}
Types Of Constructor
* In Java, constructors are special methods used for initializing objects.
* There are primarily three types of constructors:
- Default Constructor
- Parameterized Constructor
- No Argn constructor (or) Copy Constructor
- If no constructor is explicitly defined within a class, the Java compiler automatically provides a default constructor.
- This constructor is a no-argument constructor and initializes instance variables with their default values (e.g., 0 for numeric types, null for reference types, false for booleans).
- It is not visible in the source code but is inserted during compilation. *Example Program *
class MyClass {
int value;
String name;
// No constructor defined here, so Java provides a default one.
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass(); // Default constructor is called
System.out.println("Value: " + obj.value); // Output: Value: 0
System.out.println("Name: " + obj.name); // Output: Name: null
}
}
No-Argument Constructor (User-Defined):
- This is a constructor explicitly defined by the programmer that takes no arguments.
- It allows for custom initialization logic when an object is created without any specific parameters. Example Program
class MyClass {
int value;
String name;
public MyClass(int value, String name) { // Parameterized constructor
this.value = value;
this.name = name;
System.out.println("Parameterized constructor called.");
}
}
public class Main {
public static void main(String[] args) {
MyClass obj1 = new MyClass(20, "Hello"); // Parameterized constructor called
System.out.println("Obj1 Value: " + obj1.value); // Output: Obj1 Value: 20
System.out.println("Obj1 Name: " + obj1.name); // Output: Obj1 Name: Hello
MyClass obj2 = new MyClass(30, "World"); // Another call with different values
System.out.println("Obj2 Value: " + obj2.value); // Output: Obj2 Value: 30
System.out.println("Obj2 Name: " + obj2.name); // Output: Obj2 Name: World
}
}
Parameterized Constructor:
* This constructor accepts one or more arguments (parameters) to initialize the instance variables of an object with specific values provided during object creation.
* It allows for more flexible and controlled initialization.
Example Program
class MyClass {
int value;
String name;
public MyClass(int value, String name) { // Parameterized constructor
this.value = value;
this.name = name;
System.out.println("Parameterized constructor called.");
}
}
public class Main {
public static void main(String[] args) {
MyClass obj1 = new MyClass(20, "Hello"); // Parameterized constructor called
System.out.println("Obj1 Value: " + obj1.value); // Output: Obj1 Value: 20
System.out.println("Obj1 Name: " + obj1.name); // Output: Obj1 Name: Hello
MyClass obj2 = new MyClass(30, "World"); // Another call with different values
System.out.println("Obj2 Value: " + obj2.value); // Output: Obj2 Value: 30
System.out.println("Obj2 Name: " + obj2.name); // Output: Obj2 Name: World
}
}
Referred Links
Top comments (0)