Skip to content

Q 31 - Return type of Constructor #35

Discussion options

You must be logged in to vote

A constructor in Java doesn’t have a return type — not even void — and here’s why, based on both programming principles and the context from the discussion you’re viewing:


🧠 Conceptual Reason

  • Purpose of a Constructor: Its sole job is to initialize a new object of a class. It’s automatically called when an object is created using the new keyword.

  • Implicit Return: Although it doesn’t have a return type, it implicitly returns the instance of the class being constructed. This return is handled by the JVM, not by the constructor itself.

  • Syntax Rule: If you declare a return type (even void), the method is treated as a regular method — not a constructor.


🛠️ Example

class Car {
    Car(…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by AmjustGettingStarted
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants