View Single Post
Old 06-14-2007, 02:08 PM   #7 (permalink)
Vormund
 
Monster Techie

Join Date: May 2004

Location: Tucson, AZ, USA

Posts: 1,183

Vormund

Send a message via AIM to Vormund Send a message via MSN to Vormund Send a message via Yahoo to Vormund
Default Re: Help with objects in Java

That answer wasn't so simple at first...

But...saying the same thing in a different way:

You have two essential options...either have a public variable, or "helper" methods.

A public variable in your 'Player' class might be,
public static int money = 0;

Then you'd change it like,
Player playerOne = new Player( ); // your instantiated object
playerOne.money += 200;

That is the un-preferred method, for people say giving direct access to variables is bad (they could tamper with them in unwanted ways)...the other would be:

A private variable in your 'Player' class might be,
private static int money = 0;

and you have another method in this class to add money,
public static void addMoney( int amount ) {
money += amount;
}

Then to give a player money, you'd just address it with the method instead,
Player playerOne = new Player( ); // your instantiated object
playerOne.addMoney ( 200 );

And to get the money, you'd have a getter method in the Player class,
public static int getMoney( ) {
return money;
}


Both work. ...after looking at my explanation though, it's not so simple either, dang it! Anyway, hope it helps, let me know if ye' need something explained.
__________________
Vormund is offline