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!
Everything in Java is pass-by-value, but the Java definition of pass-by-value is probably a bit different than the definition you’re used to when it comes to objects.
I understand your confusion, but here’s a nice little article covering the nuances of parameter passing in Java. It’s going to sound a bit strange because, like I said, it is a bit different than the C world.
About half way through the article there’s a section labeled “Pass by Value” that specifically addresses your question, but the whole artcile can be of use.
http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html
After you’ve read the article, this next part of my post is going to make more sense:
When you pass an object parameter into a method, you’re really just passing an int (or maybe a long) that is the memory address to the object. So, when they say everything is pass-by-value, yet you can change an object within a method, they simply mean that you’re passing the object reference by value. When the method exits, the same identifier name will still point to the same object in memory, even though the values of the fields in that object may have been altered by the method.
Welcome to Java, and the games community