Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is "this" in Java ?

 
Kamal Trivedi
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this pointer.
also if there is no pointers in java,then why there are NullPointerException.
 
Henry Wong
author
Marshal
Pie
Posts: 21214
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kamal Trivedi:
Is this pointer.
also if there is no pointers in java,then why there are NullPointerException.


Java has no pointer arithmetic. Otherwise, the java reference behaves just like a pointer.

Henry
 
Kamal Trivedi
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is used internally by instance class methods..Please Explain in detail.
 
Dara Sitaramayya
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
"this" is current object of class.

Regards,
D.Sitaramayya.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"this" refers the current object of the class.

if you are calling a method directly
eg.

show();

Java implicitly uses this.show();

Also , "this" is often used in the case of shadowing ,when a local variable or a and arg has the same name as that of an instance variable.


class MyClass{
int size
setSize(int size)
{
this.size=size;
}
}

This is an incomplete program but is sufficient to understand.

this.size-> refers to the instance variable
and size on the right hand size refers to the parameter passed.

"this" differentiates between the two "size" and is always used for instance variable .
It cannot be used for static variables.

Hope that helps you to understand.
 
Paul Brabban
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think 'this' returns a reference to the instance of the class in which the code is written.

You can then use the returned reference to call methods of the class instance in which the code is written.

That's why you can't call 'this' from a static method - there is no instance of the class for which a reference could be returned.

Thanks,
 
kamal shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to my understanding its default alias reference provided by Java to every object for there(that object) internal use.

Once you create a instance of any object,Java give you default alias object reference i.e this ,referring to that object.

Now what is the use? Whenever you want to refer the current object in which you are doing something this is the only way to refer.

Example:-

Class ObjectRef{
public ObjectRef getRefOfCurrentObject() {
return this;
}
}



Is there any other way so that we can return the reference of current object sitting inside the same object...(No)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic