Does Java have global variables?

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

Was this solution helpful to you?

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:

Just Added Q & A:

Find solution

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.