Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Why Constructors dont have return type..??

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

1. Why Constructors dont have return type..??

2. Whats wrong in the following declaration??

Its a Compilation Error...

public class Pool implements Serializable extends Object ..

This works fine..
public class Pool extends Object implements Serializable ..

Thanks in Advance..

~Shridhar..
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) what would you return and how would you get at the value?
Java has no syntax like Pascal where you write MyObject o = new MyObject.create;
2) the order of implements and extends clauses is fixed and mandatory.
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I always thought it kinda did. In a manner of thinking, constructors do have a return type, that of the object they are constructing. (I know, thats not technically true, but there seems to be little difference between that and what's actually going on.) You just use the 'new' keyword rather than dot notation to call the constructor. On a lesser note, some design patterns (singletons for instance) use a getInstance method, which work alot like the MyObject.create;
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very true,
Constructors implicitly return the type of the object they have constructed and it is actually used by JVM.

The only thing is we don't need it and thus it is a lesser known fact that constructors do return a value.
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually--I hate to be a stickler here, but in fact constructors do not really have a return type. They're not really even "void". Consider the following line of code:



I'm calling the LinkedList() constructor, right? But what is it returning? The constructor is not really returning anything--it's the new operator that's doing all the returning in this statement.

But wait, isn't the constructor returning something to new, then? I don't know--the fact is, we don't know, and we're not supposed to know, whether the constructor is returning something to new or just changing the state of the system and new knows how to pick up those changes and continue on. I'm glad we're not saddled with the knowledge, as it doesn't matter and therefore would just be extra, but useless, stuff to know.

What does new do? It: (1) declares a chunk of memory for the type of object associated with the constructor, (2) runs the constructor in order to initialize that chunk of memory, and (3) hands back a reference to the newly initialized chunk of memory (which the assignment operator picks up and assigns to the reference variable foos). Note that nowhere does it really need information from the constructor to be returned, for it is the new operator that tells the constructor where to do its thing...it's not the constructor that determines where it will be loaded and which chunk of memory it is to deal with. It doesn't care, it only needs an apparently consecutive sequence of bits to flip around so it can configure itself. (Of course, new probably does a boatload of other stuff that's not mentioned here, like register the reference with the garbage collector, wrap the reference in a reference type, etc, etc, but you get the point.)

Well, if this thing doesn't really return anything, why do I say it's not really a void method either? Because "void" means something. It means, you can call me and I'll do something or other, but I won't return a value. That's not quite true either. You can't call a constructor like any other function. You can only call it in conjunction with the new operator. ("Aha!" you say, "Gotcha! Classes can call super(), and that's a call to a constructor that doesn't involve the new operator!" But, I would point out, calls to super() only happen as a result of an initiating call that is associated with new, so they too are "in conjunction" with the new operator.)

So they don't really act like a void method either. They really are special methods that have an entire operator dedicated to dealing with the contract by which they abide, so they're not void, and they're not any other return type. They're constructors, and that's all there is to it.

sev
 
Jason Fox
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're absolutely right. However, from the perspective of the developer there is usually little difference between:


and a getInstance approach:


at least as far as implementation is concerned. That the jvm is treating these two approaches vastly differently is conceded, of course it is. Thats not the point, though. The point is, both approaches get you the exact same thing, an instance. One uses the new operator, the other uses dot notation. But you're right, constructors technically do not have a return type. From a strictly development point-of-view though, they certainly act like they do.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I worked with some other language that had the syntax

Thing t = Thing.create();

That makes the constructor called create() look a lot like any other method, tho there really is some special magic to allocate an instance before the code in create() runs. The Java syntax is slightly different but essentially means the same thing. Maybe it makes the "special magic" part a little more explicit by having a unique syntax.
 
sever oon
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fear that maybe I didn't make my point as eloquently as I thought. The purpose of separating constructors out as "special" does have a purpose. It signals to the developer that there are subtle differences going on with constructors and that they follow their own set of rules. Consider constructor chaining, for example. No other kind of method does this by default...even in a language that has a Thing.create() method, superclasses of Thing must be initialized before the Thing itself.

From a Design by Contract perspective, the fact that the API presented by a constructor method is different than that of a normal method makes a difference. I would put forth that a getInstance() method is just like any other normal method...lots of methods create objects to carry out their task. This different kind of method, the constructor, is fundamental to the language, though; we can do without getInstance() methods and make do...we can't do without constructors (what would the getInstance() method call to actually create an object? :-) ).

sev
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic