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

this

sandeep Talari
Ranch Hand

Joined: Dec 24, 2007
Posts: 63
Thread t = new Thread(this);
when we give like this, actually people tells that the current Object .I am confused about the current object. Please can anybody explain me the current object
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Hi Sandeep ,
this is my try to explain you about"this" object .

As you know to call any instance method we need an instance (i.e object) of that class to invoke that method. Now , when we called that method we passed an explicit object (which is called "this" object ) which is nothing but an object on which we invoke that method ..

like



Here "object" is passed to myMethod() method of class "MyClass" , now "object" is referred as "this" object in this method..

So the bottom line is this :
In context of method , the object who called that method is treated as "this" object

Thread t = new Thread(this);
..

Now you can explain this ..


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
sandeep Talari
Ranch Hand

Joined: Dec 24, 2007
Posts: 63
ok fine , happy with your answer but ,Here "this" is passed in the constructor of the actual object which we call "new thread(this)" .Please explain what does it mean.
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Originally posted by sandeep Talari:
ok fine , happy with your answer but ,Here "this" is passed in the constructor of the actual object which we call "new thread(this)" .Please explain what does it mean.


Hi Sandeep,

First a little correction.


we call "new thread(this)"


you must specify it as "new Thread(this)" because Thread is the name of a class in java.

Then to your query,

Here "this" is passed in the constructor of the actual object which we call "new thread(this)"


Consider the following code



In this example code i am passing test object as a argument to InnerClass constructor using this keyword.

I hope now you can understand things better.
If you are still struck up don't hesitate to come back here.
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
Originally posted by sandeep Talari:
ok fine , happy with your answer but ,Here "this" is passed in the constructor of the actual object which we call "new thread(this)" .Please explain what does it mean.


Just to be sure, the object isn't called (or named) "new Thread(this)".



Thread : (the first one) is the name of the class of which you want to instantiate an object (you create an object based on that class).

t : This is the name of the reference variable that you use to access the object. t is not the object itself, but you use the variable to access the object (a bit like a remote control for your TV, sort of).

new : This is the keyword that you use to create new objects.

Thread : The name of the contructor that you call. A constructor's name is the same as the class' name.

this: The value that you pass to the constructor. By doing this, you pass a reference to the 'current object' (as you call it) to the constructor.

A bit more info on 'this': http://java.sun.com/docs/books/tutorial/java/javaOO/thiskey.html
[ June 10, 2008: Message edited by: Ronald Schild ]

Java hobbyist.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: this