Reputation:
After going on the post on this topic I found myself little confused. So again I am asking this:
"Does Java constructor returns any value?"
My books say they can't return a value, but my professor says they can and they are always doing so.
As the control needs to be transferred to someone with some value either void?
Upvotes: 32
Views: 60583
Reputation: 51037
As a Java programmer, one needs a mental model to predict the behaviour of Java programs, at both compile-time and at runtime. So long as one's mental model makes correct predictions compared with reality (i.e. compared with the behaviour the language specification describes, or compared with the behaviour of an actual Java implementation), then the mental model is viable. Two different, incompatible mental models may sometimes both be viable, and this is an example.
Here's what the language specification (§15.9.4) has to say:
Next, the selected constructor of the specified class type is invoked. ...
The value of a class instance creation expression is a reference to the newly created object of the specified class.
So the constructor is invoked, and the result of the expression invoking the constructor is a reference to the new object. The specification doesn't say that the constructor returns that reference, and it also doesn't say that the constructor doesn't return that reference.
Let's consider the "constructor returns a reference to the new object" mental model. It does make intuitive sense; when you invoke a constructor in a new Object()
expression, the expression has a value much like when you invoke a method in a foo.bar()
expression, the expression has a value. In the latter case, the expression's value is the method's return value, so it may be intuitively apppealing to say in the former case, the expression's value is the constructor's return value.
Now, it is a very clunky mental model, because you have to imagine that:
return this;
,return;
in a constructor is implicitly return this;
,return this;
is only present when it's reachable, because the static checker (which does check implicit code, e.g. in default no-arg constructors) forbids unreachable code,this();
and super();
are illegal when used as expressions, for a completely arbitrary reason.The alternative mental model, that the constructor doesn't return anything, is a lot simpler, and is viable without any caveats. As an educator, I favour teaching this mental model due to its simplicity.
But as long as you are careful, then both mental models make identical predictions about the behaviour of Java programs. Philosophically speaking, it's a bit like different interpretations of quantum mechanics; as long as they all make the same predictions, then there is no scientific basis to say that a particular one is true and the others are false. Rather, the "constructor doesn't return anything" model is just simpler and easier to apply, so in my opinion it is more useful to learn and teach.
Upvotes: 1
Reputation: 2069
The constructor returns the reference id. i have made a little program to prove it
class Alpha {
void myMethod() {
System.out.println(this);// prints same reference ID
}
public static void main(String[] args) {
Alpha alpha = new Alpha();
System.out.println(alpha);// prints the reference ID
alpha.myMethod();
}
}
Output
Alpha@16f65612
Alpha@16f6512
you can clearly see that both reference ids are same
Upvotes: 0
Reputation: 27
so, far i am concern constructor return the reference id to reference variable of that class. take an example:
class demo
{
demo{}
}
class demo1
{
public static void main(String as[])
{
demo d = new demo();
System.out.println(d);
}
}
output: demo@7d4991ad it is the reference id of the object 'd' of class demo which is returned by the constructor. if u will not define your constructor then jvm will get this id from default constructor.
you can cross check it by this line: System.out.println(new demo()); output: demo@1b6d3586. since every object has its separate heap area in memory so reference id of each object also vary.
Upvotes: 1
Reputation: 624
Constructors are SPECIAL METHODS. Well the Basic difference in Constructors and Methods is that
Constructor
whereas
Methods
Syntax For Methods:
AccessModifier ReturnType Class(...)
EG: public static void main(String []args)
Syntax For Constructors:
AccessModifier No ReturnType Class(...)
EG: public static main(String []args)
Please Note:
Java is a strongly typed language, so each function has its return type, and constructor always returns an instance of the class.
Upvotes: 5
Reputation: 2896
yes, it is the current class instance. (We cannot use return type yet it returns a value).
Upvotes: -1
Reputation: 3
Imagine yourself in a situation where you are told to sketch an Eagle.
First Scenario: You are not told how should it look. All you know is to make a simple sketch of an Eagle
Second Scenario: You are being told exactly what colors to use and the posture in which Eagle is to be sketched
Now the First scenario depicts exactly what an default constructor does and the Second scenario is when you have information as to HOW the object should be created. But until and unless you take a pencil and paper and start sketching you will not be returned anything.
So when you call the constructor using a new keyword you get an object. Though it doesnt explicitly return something but instead it creates or constructs something which you can use as an instance of a class.
Upvotes: -1
Reputation: 141
By definition there is no possibility of returning a value from a constructor.A constructor does not support any return type
Upvotes: 0
Reputation: 17
I agree with above two explanation , and want to add some statements to make more clear:
Question : What is a constructor : Answers: Its a method having name same as Class.
Question: Does constructor returns anything ? Answer: No , not even VOID.
Question : How to call a constructor ? OR how does a constructor gets called ? Answers : By creating an Object using NEW keyword.
Question: how to create object ? Answer: One way of creating object is using NEW operator , that returns an instance of object.
Upvotes: 0
Reputation: 500197
I think the confusion is purely notational. When you declare a constructor, you do it like so:
public class Foo {
public Foo() {}
}
Here, there's no explicit return value.
However, when you instantiate an object, the syntax is as follows:
Foo foo = new Foo();
This creates a new object by allocating memory and calling the constructor. Here, the result is clearly an instance of Foo
.
One way to reconcile the apparently conflicting notation is by thinking of a constructor as returning the object being constructed, but doing so implicitly.
Upvotes: 8
Reputation: 726479
This is a little confusing: constructors indeed do not return a value; it is operator new
that does. However, constructors are always used with a new
*, so it looks like they always return a value.
new
if you go through reflection. However, the same mechanisms will be in play.
Upvotes: 41
Reputation: 235984
A constructor returns a new instance of the class it belongs to, even if it doesn't have an explicit return statement.
Upvotes: 8