• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Constructor

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can I call from one constructor another constructor within same class ?
Please Explain.
 
Ranch Hand
Posts: 349
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Within the class you'd call its constructor using "this()" instead of "Demo()".
 
Author
Posts: 3473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22802
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.

I have learned lots from JavaRanch too; we all do
 
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Do not set lab on fire. Or this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic