Forums Register Login

Constructor

+Pie Number of slices to send: Send
can I call from one constructor another constructor within same class ?
Please Explain.
+Pie Number of slices to send: Send
Yes you can. But whats your purpose?

Ananth Chellathurai
+Pie Number of slices to send: Send
i was executing the below code



but getting error as Cannot find symbol

[edit]Add code tags. CR[/edit]
[ August 18, 2008: Message edited by: Campbell Ritchie ]
+Pie Number of slices to send: Send
Within the class you'd call its constructor using "this()" instead of "Demo()".
+Pie Number of slices to send: Send
Yes, use this(), this(5), etc. if you want to call you super calss's constructor use super(), super(5), etc
+Pie Number of slices to send: Send
Please Use Code Tags; I have added them to your post so you can see how much easier it is to read.

If you say this() or this(5) or super() or super(5), you can only do that as the first line of a constructor. So you can only use one of those at a time.
+Pie Number of slices to send: Send
Hi,
Yes, this could be done.But in this code you need to create a method as in the following code.


Hope this would help you.
+Pie Number of slices to send: Send
 

Originally posted by eshu khare:
Hi,
Yes, this could be done.But in this code you need to create a method as in the following code.


Hope this would help you.



That's not calling another constructor. It's calling a method with the same name as the constructors. That's a totally different thing.
+Pie Number of slices to send: Send
A quite bad thing even. Using methods with the same name as the constructor will only confuse everybody who will read your code - including yourself in the future.
+Pie Number of slices to send: Send
Hi,
So please let me know that if I dont use super or this ,then what would be more suitable way to write the above asked code.
[ August 18, 2008: Message edited by: Ulf Dittmer ]
+Pie Number of slices to send: Send
 

Originally posted by eshu khare:
So please let me know that if I dont use super or this ,then what would be more suitable way to write the above asked code.



Depends what you are trying to do.
Are you trying to call another constructor - if so use this as described in previous posts.
If you just want to call a method, then your code will work. Rob was just saying that it would be better not to use the class name as the method name as it makes the code less readable. People might mistake the method for a constructor if they just glance quickly at the code.
+Pie Number of slices to send: Send
If your class extends another class without a ******** constructor with no arguments, you must use super(something) somewhere. In that case any constructors which don't call super(something) as their first line must call this(something) so as to get to super(something) via the other constructors.

You call this(something) to pass parameters from one overloaded constructor to another in the same class. There is a Clock class which appears in P Deitel and H M Deitel Java How to Program. I am writing something similar to that class to demonstrate this()Without using this() to access the overloaded constructor it would read like this

[edit]Delete the word "declared" where the ******** now is.[/edit]
[ August 18, 2008: Message edited by: Campbell Ritchie ]
+Pie Number of slices to send: Send
Hi Guys,
Thanks,though it was not my doubt even then got many things to learn;this is what Big Moose Saloon is.
+Pie Number of slices to send: Send
You're welcome.

I have learned lots from JavaRanch too; we all do
+Pie Number of slices to send: Send
if a call is to be made to a constructor in the same class or a constructor in the superclass, it should always be the first statement inside the constructor, so this code will run:
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1371 times.
Similar Threads
Valid places to call a constructor from
Verification of constructor calling
Calling constructor
Constructors invocation
Assigning values to arguments? (Modules and Methods?)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:21:56.