Constructor and Constructor’s Return Type in Java

Constructors are special methods used to initialize objects in object-oriented programming. Unlike other methods, constructors do not have an explicit return type. The return type of a constructor is always the same as the class type itself.
If you don’t have a medium membership, you can use this link to reach the article without a paywall.
For example, consider the following simple class in Java:
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
}The Person class has a constructor that takes a String parameter name. This constructor initializes the name field of the Person instance being constructed.
Note that the constructor does not have a return type explicitly specified. However, the return type is implicitly the Person class itself. When you construct a Person object using new Person(“John”), the constructor creates and returns a new Person instance.
Return Type of Constructors
The reason constructors have an implicit return type is that their purpose is to initialize newly allocated objects. The new keyword allocates memory on the heap for the new object and then invokes the constructor to initialize this object.
Since constructors always return the object they are constructing, there is no need to explicitly specify the return type. It is implicitly understood.
This implicit return type is convenient, as it reduces redundancy in the constructor declaration. However, it does mean that constructors cannot have an explicit return type other than the class type. Attempting to specify a different return type will result in a compilation error.
For example, this constructor declaration would be invalid:
public String Person(String name) { // Compiler error
...
}Specifying the return type as String rather than having the implicit Person return type causes a compilation error.
The constructor also cannot have a void return type. For example:
public void Person(String name) { // Compiler error
...
}This is invalid because the constructor must return a Person instance, not void.
It is possible for a constructor to not return at all by throwing an exception. For example:
public Person(String name) {
if (name == null) {
throw new IllegalArgumentException("Name cannot be null");
}
this.name = name;
}Here, if a null name is passed, the constructor will throw an exception and not finish initializing the object. However, the compiler still understands the implicit return type is Person.
In summary, the key points about the implicit return type of constructors are:
- Constructors do not specify an explicit return type.
- The return type is implicitly the same as the class being constructed.
- You cannot change this return type to something other than the class type.
- Constructors cannot have a void return type.
- Constructors can throw exceptions before returning, but the implicit return type remains the class type.
- The implicit return type reduces redundancy since the constructor’s purpose is to initialize class instances.
Understanding this implicit behavior is important when designing constructors for your classes. Keep in mind that even though no return type is specified, you should ensure the constructor properly initializes the object state before returning the newly constructed instance.
👏 Thank You for Reading!
👨💼 I appreciate your time and hope you found this story insightful. If you enjoyed it, don’t forget to show your appreciation by clapping 👏 for the hard work!
📰 Keep the Knowledge Flowing by Sharing the Article!
✍ Feel free to share your feedback or opinions about the story. Your input helps me improve and create more valuable content for you.
✌ Stay Connected! 🚀 For more engaging articles, make sure to follow me on social media:
🔍 Explore More! 📖 Dive into a treasure trove of knowledge at Codimis. There’s always more to learn, and we’re here to help you on your journey of discovery.






