Okay I have 3 classes. One of them called player has an array list with two methods that can add and delete things from the array list. Another is called shop. The player array list has values set and removed within this class using the player methods. After that class is executed it goes back to the main class. I decided to print out the player array list but it gets reset to nothing after you buy something in a shop. Is there any way to keep the values from the shop class and bring them over to the other classes? If you need it heres my code: *EDIT* Just to make this clear I know i'm making a new player object. I need to know how to set it to the same values as the shops player object.

There are also a couple ways to do this, like setting a getInventory() method in your player class, which returns the inventory. And actually, you don't even need to do this at all. Since you instantiated the inventory in player as public, you can access the inventory in any other class by calling player.inventory

Another way to do it is by making inventory public and static.

Code java:

publicstatic ArrayList<String> inventory =new ArrayList<String>();

Now, whenever you want to access the inventory from a different class, just call "player.inventory".

However, some things to clear up:

It's a convention to make all of your Java classes uppercase, not lowercase. This allows things like "player player = new player()" be less confusing; "Player player = new Player()" helps you identify which one is the class and which one is the object.

This is important, because the method I mentioned earlier that sets the inventory to static uses the class, and not the object to access the inventory. By making inventory static, it means that there is now only one instance of it. No matter how many classes you had of Player, they would all use the same inventory. If your class was "Player" rather than "player", your code to access the inventory would look like this: "Player.inventory"

January 23rd, 2011, 09:30 PM

sp11k3t3ht3rd

Re: Using values set in one class in another

Thanks a ton! I would have never figured that out!

January 23rd, 2011, 09:34 PM

dpek

Re: Using values set in one class in another

No problem, glad you got it solved. I would suggest looking more into class-specific things in java like public, private, protected, static, void, get/set methods, what the args in public static void main(String[] args) is, extends, implements, Interfaces, etc. There's a lot out there that will be very helpful for your project.