Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused with constructors concept.

 
Joy Vergis
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff for clearing the point...

 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic