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.
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.
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 ]
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.
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 ]
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 by:autobot
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