• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

usage of "this"

 
Jeet Dholakia
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys,

im learning java and i know this is a pretty basic question,well all books and documentation say "this" keyword is used to refer to the object calling the method and also to call a super constructor, its the first use that bothers me, can anyone give me a practical and easy example to understand it?

thanks,
jeet
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, there is a reasonable example given here.

When used with constructors you call another constructor in the same class in question using this() - not the superclass constructor (use super() for that). This is also explained in the link.

Just to add that if you don't use this() or super() explicitly in a constructor then super() is called by default anyway. If you want to pass parameters to the superclass constructor you need to use super([params]).
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Tony Docherty
Bartender
Posts: 2953
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well all books and documentation say "this" keyword is used to refer to the object calling the method

I doubt if they say exactly that. They probably say something like 'this' can be used to refer to any method in the current object.
Check out the Java Tutorial http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
 
Jeet Dholakia
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phil English wrote:Hey, there is a reasonable example given here.

When used with constructors you call another constructor in the same class in question using this() - not the superclass constructor (use super() for that). This is also explained in the link.

Just to add that if you don't use this() or super() explicitly in a constructor then super() is called by default anyway. If you want to pass parameters to the superclass constructor you need to use super([params]).

Tony Docherty wrote:
well all books and documentation say "this" keyword is used to refer to the object calling the method

I doubt if they say exactly that. They probably say something like 'this' can be used to refer to any method in the current object.
Check out the Java Tutorial http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html



thanks for the explanation guys,

Im starting to understand the use cases slowly,although some advanced uses like the one below confuse me



Kemal Sokolovic wrote:And welcome to the Ranch!

anyways,thanks for the help,i came across this forum in the book "head first java" and im happy i did!
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeet Dholakia wrote:some advanced uses like the one below confuse me


You're probably thinking it's something more complicated than it really is. Let's look at it this way. Every class has an instance method toString(), inherited from Object and in many cases overridden in the class or a superclass to return a String that reveals useful information about the instance. Now consider the String class. What's the most appropriate value to return from toString()? Why, the String itself!

And in fact that's what the method does. From the source (which you can find in src.zip in your JDK installation folder):
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:They probably say something like 'this' can be used to refer to any method in the current object.


Um, what?
 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


so 'this' can be thought of as a variable that points to the current instance of the class, the most advanced usage I can think of would be using the 'this' keyword to call another constructor from a constructor within the same class, which is covered (as is all of this) in the tutorial referenced earlier
 
Jeet Dholakia
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for those examples guys, im now clear as to how and where to use it, if i do have some problem with "this" ill post back!

indeed,this is the right forum i came across

thanks!

Jeet
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic