aspose file tools*
The moose likes Beginning Java and the fly likes Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor" Watch "Constructor" New topic
Author

Constructor

shruti patel
Ranch Hand

Joined: Mar 10, 2008
Posts: 52
can I call from one constructor another constructor within same class ?
Please Explain.
Ananth Chellathurai
Ranch Hand

Joined: Nov 21, 2007
Posts: 349

Yes you can. But whats your purpose?

Ananth Chellathurai


Ananth Chellathurai [Walk on software]
shruti patel
Ranch Hand

Joined: Mar 10, 2008
Posts: 52
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
Marshal

Joined: Mar 22, 2005
Posts: 39571
    
  27
Within the class you'd call its constructor using "this()" instead of "Demo()".


Ping & DNS - updated with new look and Ping home screen widget
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3216
Yes, use this(), this(5), etc. if you want to call you super calss's constructor use super(), super(5), etc


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36566
    
  16
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

Joined: Aug 01, 2008
Posts: 44
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.


Thanks & Regards
Eshu Khare
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
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.


Joanne
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19552
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
eshu khare
Ranch Hand

Joined: Aug 01, 2008
Posts: 44
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

Joined: Aug 05, 2005
Posts: 3169
    
  10
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

Joined: Oct 13, 2005
Posts: 36566
    
  16
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

Joined: Aug 01, 2008
Posts: 44
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

Joined: Oct 13, 2005
Posts: 36566
    
  16
You're welcome.

I have learned lots from JavaRanch too; we all do
Janardan Kelkar
Ranch Hand

Joined: May 05, 2008
Posts: 72

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:


When the compiler's not happy, ain't nobody happy.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor
 
Similar Threads
IS CONCTRUCTOR IS A METHOD
main method
WA #1.....word association
Constructor doubt - K&B Book
Can I overload a constructor with enum values?