aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What is Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What is "this" in Java ?" Watch "What is "this" in Java ?" New topic
Author

What is "this" in Java ?

Kamal Trivedi
Ranch Hand

Joined: May 22, 2006
Posts: 38
Is this pointer.
also if there is no pointers in java,then why there are NullPointerException.


Kamal.B.Trivedi
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kamal Trivedi
Ranch Hand

Joined: May 22, 2006
Posts: 38
this is used internally by instance class methods..Please Explain in detail.
Dara Sitaramayya
Greenhorn

Joined: Feb 26, 2008
Posts: 3
Hi,
"this" is current object of class.

Regards,
D.Sitaramayya.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
"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.


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Paul Brabban
Ranch Hand

Joined: Jan 13, 2008
Posts: 44
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,


"The important thing is not to stop questioning."
kamal shah
Greenhorn

Joined: Feb 26, 2008
Posts: 18
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)


Regards,<br />Kamal<br /> <br />Life is nothing but a competition to be the criminal rather than the victim -Bertrand Russell
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is "this" in Java ?