File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Confused with constructors concept. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confused with constructors concept." Watch "Confused with constructors concept." New topic
Author

Confused with constructors concept.

Joy Vergis
Ranch Hand

Joined: Sep 14, 2009
Posts: 45
Constructors are used to set the initial state of an object. Please help me clarify below queries

Please correct me if my understanding is wrong.




Query1: Regarding below declaration. What can be the reason for allowing the declaration at line no(1A).



Query 2: Allow return in tis simple form in constructors. What is their use at line. (1A) below if they do not return anything not even void.




Regards,
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

Regarding the first one - yes, that particular construction is bad. But on what grounds should the compiler prevent it? There are valid situations where you might want to create an object of type A in the constructor of type A. Yes, you want to make sure it doesn't happen in all cases, to prevent infinite recursion, but that's the same as any recursive method.

The second one - the use of a return statement would be to exit the constructor at that point - exactly the same as using return in a void method. It would always be possible to rewrite the constructor to avoid using it (using suitable if statements), but it's valid. There's no point using it like the example given, though - adding it at the end of the constructor is pointless.
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 191

Constructors are used to set the initial state of an object.

I don't think that constructors are use for this reason. they are used to initialize the object in the memory. however you can set the instance variables of the object.

Query1: Regarding below declaration. What can be the reason for allowing the declaration at line no(1A).


when you are calling the constructor inside a constructor you are calling a recursive function but there should be some returning condition when we are using the recursive technique so that we meet some end point and come back to our original call, but there is no such condition so I have doubt about this that will run successfully(will not stuck in infinite call).

Query 2: Allow return in tis simple form in constructors. What is their use at line. (1A) below if they do not return anything not even void.


for this as I can understand this is just to return back the control to the calling method. but even if you don't specify return, there will be default return by compiler, like we have default constructor by compiler if we don't define any. but I am not sure about this. We need some comments from experts..


Do not wait to strike till the iron is hot; but make it hot by striking....
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Manoj Kumar Jain wrote:
Constructors are used to set the initial state of an object.

I don't think that constructors are use for this reason.


That is exactly what they are used for.

they are used to initialize the object in the memory


You mean create the object? No, the new operator does that.

. however you can set the instance variables of the object.


That's what "set the initial state" means. A c'tor's job is to put a newly created object into a valid initial state.

Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 191

Thanks Jeff for clearing the point...

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Manoj Kumar Jain wrote: . . . when you are calling the constructor inside a constructor you are calling a recursive function . . .
No, you aren’t. There is nothing recursive about constructor calls, and they are not functions.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote:
Manoj Kumar Jain wrote: . . . when you are calling the constructor inside a constructor you are calling a recursive function . . .
No, you aren’t. There is nothing recursive about constructor calls


Well, it depends what he meant.

If Foo's c'tor invokes new Foo(), then yes, that's recursion--or can be, depending on which signatures create new Foos with which signatures. We're not, of course, directly calling a c'tor. We're invoking the new operator which in turn calls the c'tor.

If you directly invoke a c'tor using this(...), however, that can never be recursion. If you write it such that it would be, the compilation fails.
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 191

what OP is doing he is calling Example() inside the Example(), so isn't it a infinite recursion. I think this is the non ending call. however if he called Example() constructor inside the Example(int a)(parametrized constructor) then yes its not infinite loop.

No, you aren’t. There is nothing recursive about constructor calls, and they are not functions.


aren't the c'tor are just functions/methods without return type ?? what I know is that the c'tor is just like a special function/method which don't have return type.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Manoj Kumar Jain wrote:what OP is doing he is calling Example() inside the Example(), so isn't it a infinite recursion.


Yes.

No, you aren’t. There is nothing recursive about constructor calls, and they are not functions.


aren't the c'tor are just functions/methods without return type ??


Not according to the JLS.

what I know is that the c'tor is just like a special function/method which don't have return type.


It's not just like a method. For one thing, you can't just arbitrarily call it whenever you want. For another thing, it's not inherited. And, as mentioned above, the JLS talks about methods and c'tors separately, and never says the a c'tor is a "special method".
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Confused with constructors concept.