Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialLi Yu
2,067 PointsReturn a value
I am wondering why constructors cannot return a value. Does "returning a value" means to change the value after initializing the constructor?
1 Answer
Steven Parker
231,198 PointsConstructors establish an object instance but do not directly return a value.
You would typically call a constructor like this:
var newobject = new SomeObject();
So you would not see a return value (even if there was one) from the constructor, but the new operator returns the constructed object. So, in a sense, you could say a constructor always "returns" the object.
If you really need to get a value back from a constructor, you can pass it an "out" argument.
Li Yu
2,067 PointsLi Yu
2,067 PointsThanks for the answer, Steven.