I have started java a month ago. And I have a little concept of some basic C++. Now while studying classes I came across a method this()whose logic, I don't seem to pick up. Can anybody please
it makes sense since you come from c++ you wouldnt get that method. java uses the this() method so u can call one constructor from another constructor. you cant do that in c++ but it looks for exmaple like this:
you get the idea? you can call one constructor from another in the same class to save some code writing (if the constrcutors have a simliar code to be written). this() without anything as parameter calls the empty constructor in the class. just as a last note. this() must be the first line in the constructor you are using and you can't have super() with it. [ April 29, 2002: Message edited by: Roy Ben Ami ] [ April 29, 2002: Message edited by: Roy Ben Ami ]
hello there this() is used for constructor chaining for example..if u want to call a single argument constructor from within a zero argument constructor. class Tiger{ Tiger(){ this(6); //must be the first line of a } //constructor!
Can anyone help me about and give me a short example about the difference between this() and super? thanks so much! =) coz I am stil lconfused after reading the teh contructor call for this and super
the one major rule with this() and super() is that it has to be the FIRST statement in the constructor definition (if you're using it). -- so because of that -- you can't use both this() and super() in the same definition.