This write-up aims to explain the below-listed concepts of Java constructors:
- What is a Java Constructor?
- How to create a constructor in Java?
- Method VS constructor
- Practical implementation.
So, let’s get started!
What is a Java Constructor?
In Java, constructors are special/unique methods that assign some initial values to an object at the time of its creation. In Java, all classes must have a constructor, irrespective of whether a user defines a constructor or not. If a user defines a constructor, then the object will be initialized with the user-specified values. Otherwise, the object will be initialized as 0.
How to create a Java constructor?
You have to follow the below-listed standards to create a Java constructor:
- The constructor’s name should be the same as the class name.
- Syntax:
class constructorExample{
constructorExample(){
//statements;
}
} - You can not specify any return type while creating a constructor in Java.
- You can’t create a java constructor with abstract, static, synchronized, or final keywords.
Method VS constructor
The below-given table will provide a comparative analysis of a Java method and a constructor:
Constructor | Method |
Initializes an object. | Shows the functionality of an object. |
Doesn’t return anything. | Can return value. |
Must be declared with a similar name as the class name. | It can be declared/created using any user-specified name. |
The compiler creates a default constructor for each class in java. | Java Compiler does not provide a default method for any class. |
Invoked automatically. | Invoked explicitly. |
Practical implementation
Until now, we have learned the theoretical concepts of Java Constructors. Now, we will learn how to create a constructor practically with the help of some examples:
Example:1 how to create a default constructor in Java?
ExampleClass(){
System.out.println("Object Created Successfully");
}
public static void main(String[] args) {
ExampleClass obj = new ExampleClass();
}
}
In this example program, we created a default constructor, which will show a message “object created successfully” whenever an object of the class is created:
The output verifies that the default constructor gets invoked automatically when we create an object of the class. The constructor initialized the object with a value specified within that constructor.
Example:2 how to create a parameterized constructor in Java
int empAge;
String empName;
ExampleClass(int age, String name) {
empAge = age;
empName = name;
System.out.println("Employee Age: " + empAge);
System.out.println("Employee Name: " + " " + empName);
}
public static void main(String[] args) {
ExampleClass obj = new ExampleClass(27, "Joe");
}
}
In this program, we created a parameterized constructor. We passed some values to the constructor at the time of creating the class object. On successful execution of the above-given program, we observed the following output:
The output verified that the constructor initialized the object with values passed at the time of object creation.
Conclusion
To create a constructor, we have to follow some rules i.e. The constructor’s name should be the same as the class name and It must be declared/created without any return type. A java constructor can’t be created with the abstract, static, synchronized, or final keywords. In Java, constructors are specialized methods that assign some initial values to an object at the time of its creation. In Java, all classes must have a constructor, irrespective of whether a user defines a constructor or not. This write-up considered some examples to explain the working of Java constructors.