• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor

 
shruti patel
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can I call from one constructor another constructor within same class ?
Please Explain.
 
Ananth Chellathurai
Ranch Hand
Posts: 349
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can. But whats your purpose?

Ananth Chellathurai
 
shruti patel
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Within the class you'd call its constructor using "this()" instead of "Demo()".
 
arulk pillai
Author
Ranch Hand
Posts: 3387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, use this(), this(5), etc. if you want to call you super calss's constructor use super(), super(5), etc
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
eshu khare
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
eshu khare
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
eshu khare
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
Thanks,though it was not my doubt even then got many things to learn;this is what Big Moose Saloon is.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.

I have learned lots from JavaRanch too; we all do
 
Janardan Kelkar
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic