wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes usage of Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "usage of "this"" Watch "usage of "this"" New topic
Author

usage of "this"

Jeet Dholakia
Greenhorn

Joined: Dec 15, 2012
Posts: 25
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

Joined: Jun 18, 2012
Posts: 62

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

Joined: Jun 19, 2010
Posts: 825
    
    5

And welcome to the Ranch!


The quieter you are, the more you are able to hear.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2172
    
  47
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

Joined: Dec 15, 2012
Posts: 25
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

Joined: May 03, 2008
Posts: 4523
    
    5

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):


luck, db
There are no new questions, but there may be new answers.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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

Joined: Nov 18, 2007
Posts: 97



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


www.mormon.org
Jeet Dholakia
Greenhorn

Joined: Dec 15, 2012
Posts: 25
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
 
 
subject: usage of "this"
 
Similar Threads
clear JTextArea
JTextPane retrieve changed data from HTMLDocument
Please advice for a better Career Growth
How to show image in a https jsp page from local system
Applet and Servlet