• 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

Confuse about "this" keyword

 
Greenhorn
Posts: 10
Android Netbeans IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm really confused about "this" keyword in this situation...Could you please explain it in details? It would be appreciate!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this refers to the current object (the object that the method that is executing, was called on).

In line 18, you create a new Apple object which is passed to the eat method of class Person.
In line 3, you call getPeeled on that Apple object.
In line 14, the Apple object calls Peeler.peel, passing itself to that method.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "this" keyword indicates the current instance. In your code, it is the Apple instance current with the Peeler which the Person is going to eat

To simplify


this.age on line 4 refers to the age defined on line 2. So essentially you say assign the value of age on line 3 to the age defined on line 2.

More details here http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The 'this' keyword is used to refer to the current instance - in your case, an instance of the class Apple.

Using the this Keyword
 
Lucky Lam
Greenhorn
Posts: 10
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So instead of using "this", is there other way to perform? Thanks so much!
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the peel method in the Peeler class takes an Apple instance as an argument. So nothing stops you from passing in a different Apple instance.

However, the Apple class is delegating the action of peeling to the Peeler class (in it's gelPeeled method) so there is no substitute for using the this keyword here.
 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This was already replied in any possible way .
My reply is:
Keyword this refers to instance of enclosing class.
 
reply
    Bookmark Topic Watch Topic
  • New Topic