THIS - java

Questions about programming languages and debugging
Post Reply
User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
15
Contact:

THIS - java

Post by l0ngb1t »

well i was really sick and i had the skip some class, including a java class :cry: 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... :wink: and on monday i have a have a java exam...
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"

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Post by leetnigga »

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:

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;
    }
}
'this.name' refers to the 'name' instance variable (String name;) in the object (which is an instance of Website).

Code: Select all

Website suck_o = new Website();
suck_o.setName("www.suck-oold.com");
suck_o.getName(); // returns "www.suck-oold.com"
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.

User avatar
Stavros
ΜΟΛΩΝ ΛΑΒΕ
ΜΟΛΩΝ ΛΑΒΕ
Posts: 1098
Joined: 02 Jan 2006, 17:00
18
Location: Mississippi, U.S.A.

Post by Stavros »

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:

Code: Select all

//Account Contstructor 1
public Account (String name, long account, double balance)
{
     name = owner;
     acctNum = account;
     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:

Code: Select all

//Account Contstructor 2
public Account (String name, long account, double balance)
{
     this.owner = owner;
     this.account = account;
     this.balance = balance;
}
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.

User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
15
Contact:

Post by l0ngb1t »

heheheh it's ok stavros lol
and thanks guys for the help
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"

Post Reply