File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a beginner wanna knows something about this( )

 
Fahad Golra
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Khurram Ajmeri
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

Tiger(int i){
System.out.println("Tiger");
}
}

if u are still confuse please read the book.
 
Jackie Wang
Ranch Hand
Posts: 315
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jackie Wang
Ranch Hand
Posts: 315
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean i still don't understand after reading some notes about the contructor call, it make me more confused

Originally posted by jackie Wang:

coz I am stil lconfused after reading the teh contructor call for this and super
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"this" is for the same class.
"super" is for the class that the "this" class extends (inherits from), in other words, its "super" class.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jackie Wang
Ranch Hand
Posts: 315
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks jessica and Marilyn for the prompt reply.
I really appreciated that.
Any simple example which can demonstrate the "super()"? Thanks so much
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try running this blip of code -- with the println statements you'll be able to see the flow of operation:

Output:
Instantiate MySuper( "blah" );
MySuper() default constructor
MySuper( blah )
Instantiate MySub( "stuff" )
MySuper() default constructor
MySuper( stuff )
MySub( stuff )
 
Jackie Wang
Ranch Hand
Posts: 315
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks dirk and jessica. I got it now.
really appreciated that
 
Fahad Golra
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank u all for making me understand this topic. Thank u all again
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As if [fill in some saying], here's a link to part of Sun's java tutorial that discusses this and super a bit: http://java.sun.com/docs/books/tutorial/java/javaOO/methodbody.html
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic