Hello. Im Developing a lemonade based game and i have a question. How do you pass information through classes is basically it. The first class handles the menus that occur between game days (what inventory do you want to purchse…type question) the second class handles the animation and the processing of the game day information. I have a third class called inventoryItem. This class is created for each inventory item to handle things like the number of that item, ect.
–> inventoryItem Hamburger = new inventoryItem(); //ex
How can I pass the inventoryItem to the second class, make changes to it and then get those change back to the first class and visa versa? (Im a c++ programmer and java is weird to me without the pass by value/reference stuff in c++)
Any help would greatly be aprreciated!
[quote]Cook cook = new Cook();
InventoryItem hamBurger = new HamBurger();
InventoryItem cheeseBurger = cook.addChese(hamBurger);
[/quote]
This is a really basic OO question. You might want to readt the Java Tutorial:
http://java.sun.com/docs/books/tutorial/
specifically:
Learning the Java Language
http://java.sun.com/docs/books/tutorial/java/index.html
Here’s the trivk, froma C++ POV.
All Java calls are calls by value HOWEVER
an object is not the value on an object variable. An object reference is the value.
So if I have
Object foo = thisobject;
Then the value of foo is a reference (think pointer in c++) to the same object that thisobject references.
so if barobject has a method
public method mycall(Object bar){
bar.intfield = 1;
}
and it is called as follows
barobject.mycall(foo)
Then the obejct known to the caller as foo will get its intfield set to 1.
See?
The thing to remember is that Object variables are references TO objects, not the objects themselves. So although the variable is passed by value, that value IS a reference (like a C/C++ pointer).
Got it?
mycall ends up with a referene to the same obejctfoo referenced. Any methdo invocations