File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this

 
sandeep Talari
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ..
 
sandeep Talari
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 215
Firefox Browser Linux VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic