• 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

can you teach me " this " keywords second use ?

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi , i know that " this " keyword or reference is used for two ways

i had understand the first one but i didnt understand the second one , i have google it but didnt find what i was looking for , so please any one here can teach me the second use of " this " keyword
with a simple and sweet " sample program " as i m just a java beginer , just 2 weeks ago i had started learning java.
thanksss.
 
Ranch Hand
Posts: 38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you need to grasp is that the "this" keyword in a Java program always refers to the object on which the method you're currently in was called (the callee of the method).

So to illustrate with the help of an example, suppose that you have a Shape class with a getRenderer() method, where getRenderer() returns a Renderer object that can draw the shape onto the screen.
Suppose further that the Renderer class has a constructor that takes as a parameter the Shape object that it should draw. Then the getRenderer() method of the Shape class might look like this:



So this method returns a new Renderer object that draws the Shape object on which the getRenderer() method was called (this).
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is another use of the this keyword that you did not yet mention. Watch this:

You can use it in constructors to call one of the other constructors in the class.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:There is another use of the this keyword that you did not yet mention. Watch this:

You can use it in constructors to call one of the other constructors in the class.


thanks ..can you also explain me

i didnt understand . properly yet .
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try serialisation:In the case of this serialise method, you are passing the whole object to the writeObject() method with the this keyword.
 
Jesper de Jong
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

naved momin wrote:thanks ..can you also explain me

i didnt understand . properly yet .


That is what Bernhard explained above.

You can regard this as a kind of special variable, that refers to the object that the currently running method was called on. Here is another example:

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:

naved momin wrote:thanks ..can you also explain me

i didnt understand . properly yet .


That is what Bernhard explained above.

You can regard this as a kind of special variable, that refers to the object that the currently running method was called on. Here is another example:


now 80% is clear , but i didnt understand what do you mean by

what is hapening over here is we have created two reference variable i.e e and example which points to the same object i.e " new Example() ", and we are calling methodOne() in which we have specified but what does " this " is here ...do we are passing "example reference-variable " here to " another reference-variable e " as an argument .
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can compare "this" inside an object to yourself talking about "me".
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you call a (non-static) method, you always call it on some object. Look at the main method in my example:

In the second line, we are calling methodOne on example. When we then go to methodOne, then inside the method this will refer to the Example object that methodOne was called on.

To translate it to human language, using Rob's suggestion:

Line 19: Main says: "example, execute methodOne!"
Line 10: example now gets to methodOne, and it says: "methodTwo, execute, I'm passing you myself (this)!"
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello to all,
'this' keyword has only one meaning that it has the address of currently calling object or intance. Because it stores the address of currently calling object so it has multiple uses which can be.....we can initilize the fields, this can also be passed in constructors as a parameter....or in some method...etc
i will show you example which will represent the applications of 'this' keyword..




1)
class A
{
int a,b;
public void set()
{
this.a=5;
this.b=6;
}
}
class B
{
public static void main(String s[])
{
A obj=new A();
obj.set();
System.out.println(obj.a+""+obj.b)
}
}
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic