• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

usage of "this"

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 62
Netbeans IDE MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]).
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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):
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 97
Python VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic