What is the best way to store variables in Java?
-
This might seem like a weird question, but here it goes. I'm making my first Java game right now (or, I have been for awhile). In smaller, simpler programs, keeping track of variables was easy, because it was usually only one or two classes, and a handful of methods. But now, I have almost nine classes already, and I'm running into issues with needing "global variables". For example, I have a variable that tells the "status" of the game, running, paused, or shutting down. This variable needs to be accessible to the Graphics, Logic, and Control threads, so they will shutdown with the program. My initial thought was that I would just get the variables from these methods in various classes. That however, was stupid, because non-running classes/methods re-initialize the variable's value when they are called again, and using static variables is bad code and unpredictable. So, is there some method I'm missing for gettingĀ a variable's value between different classes, or do I need to write out all variables to temp files? My thinking is that writing everything to a temp file, and having all classes read that file, would cause conflicts in read/write as well as slow the program down tremendously. Then again, the first thing I programmed on was a TI-83, and computers are quite a bit faster than that. Help?
-
Answer:
You can store the values in a Game singleton. Like Game.getInstance().setStatus(status) and getStatus, respectively.
Pavel Mitrofanov at Quora Visit the source
Other answers
Look at using Guice[1] or other method of dependency injection (you don't always need a framework). Manually created singletons as suggested by are an option, but have the drawback of difficulty of isolation for testing. http://code.google.com/p/google-guice/
Alex Feinberg
There is a reason frequent use of static fields is frowned upon, and that reason is that static fields are less flexible than non-static ones, because there can only be one copy of a static field, but a non-static field can hold a different value for each object instance. By using static fields, you are restricting yourself to only ever have one player. If you ever want to do multiplayer mode, you can't - without rewriting a lot that is. Make sure to check Variable section out for more clarification: @http://learnjavaeasy.com/course/learn-java-online-with-java-8-by-examples/ Best of luck!
David Brad
getter and setter methods are better suited for this kind of situations for example,if you want to know whether the game is paused or not,you can create a class called Status with methods getStatus and setStatus
Mahmoud Hossam
Related Q & A:
- What is the best way to distribute an audio/video feed from a computer to TVs over existing indoor coax cable?Best solution by Audio-Video Production
- What is the best way to clean LEGO bricks?Best solution by bricks.stackexchange.com
- What is the best way to make UI for an Isometric game in Java?Best solution by Game Development
- What is the best way to calculate a date difference?Best solution by Stack Overflow
- Best way to store belongings?Best solution by lifehacker.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.