well i was really sick and i had the skip some class, including a java class they explained the "this"...
i searched for some site about it but i really like to understand it with a suck-o method
any one can give a little explanation ???
don't say why i don't ask one of my class, well they are dumb asses and i don't blend with them alot... and on monday i have a have a java exam...
THIS - java
THIS - java
There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly. "The Jester"
I have Java in school too at the moment.
'this' is simply a reference to the object that you're using it in.
An example:
'this.name' refers to the 'name' instance variable (String name;) in the object (which is an instance of Website).
It's true that for example in getName method of the Website class above you can just 'return name;' but you can see the advantage of using 'this' in setName, namely that you don't have to pick an other name for the method argument.
'this' is simply a reference to the object that you're using it in.
An example:
Code: Select all
public class Website
{
String name;
/* other code here */
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
Code: Select all
Website suck_o = new Website();
suck_o.setName("www.suck-oold.com");
suck_o.getName(); // returns "www.suck-oold.com"
I don't remember going over the this reference last semester. Anyway, I'll be paraphrasing from the book we used.
Basically the this reference allows an object to reference itself.
Let's say you have a constructor Account that has three variables (name, acctNum, initial) and you pass it the owner of account, account number and initial balance:
This way we have different variable names for the constructor variables and instance variables. Instead thiscould be used and we wouldn't have to have seperate variable names:
When used in an object this only refers to that object through a method. Say if you had two objects bishop1 and bishop2 (like on a chess board) they both have a method move. If you used this in a method in bishop1 then this refers to bishop1 object, but if it were used in a method in bishop2 then it refers only to bishop2 object.
Hope that helps.
Damn beaten by 7 minutes.
Basically the this reference allows an object to reference itself.
Let's say you have a constructor Account that has three variables (name, acctNum, initial) and you pass it the owner of account, account number and initial balance:
Code: Select all
//Account Contstructor 1
public Account (String name, long account, double balance)
{
name = owner;
acctNum = account;
initial = balance;
}
Code: Select all
//Account Contstructor 2
public Account (String name, long account, double balance)
{
this.owner = owner;
this.account = account;
this.balance = balance;
}
Hope that helps.
Damn beaten by 7 minutes.