Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Beginner: this

Status
Not open for further replies.

aas1611

Programmer
Dec 14, 2001
184
DE
What does 'this' funktion/method do? I have tried to look at some tutorials but the explanations are mostly too vague.

Could you guys maybe also recommend me some good sites about how to learn Java 2?

thanks!!
 
this refers the the object instance itself.
Code:
public class MyClass {
   private int someValue;

   public void setSomeValue(int someValue){
      this.someValue = someValue;
   }
}

In this example the this.someValue refers to the instance variable at the top of the class, whereas the someValue it is assigned refers to the method parameter.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Java is an object orienated language, so if I have an object called 'foo' which has a method called 'bar' then I call the method by foo.bar()

The keyword 'this' is a way for the object to refer to itself. In timw example above, the line

this.someValue

means the attribute someValue that belongs to 'this' object instead of the attribute someValue that was passed to the method.

It may sound a bit weird but if you readup on object orientated languages it may start to become clearer.

Hope this clarifies things a bit :-s

Looking for a job as a programmer in Bristol UK.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top