Using Static Variables to Store State Across Instantiations

There are several situations when you need to retain the state of a flag between different instances in a program. The standard way to do this is to declare the flag static. The state of the flag will be maintained across multiple instantiations of the class. However, the static flag is maintained only as long the class is not reloaded. As a result, whenever you run the program afresh, the flag will lose its state.

 1.   public class TestFlag {2. 3. private static boolean myStaticFlag_ = false;4. 5.     public boolean  getFlag () {6.       return myStaticFlag_;7.   }8. 9.   public void  setFlag (boolean myStaticFlag) {10.     myStaticFlag_ = myStaticFlag;11.   }12. 13.   public static void main (String[] args) {14. 15.     TestFlag tf1 = new TestFlag();16.     System.out.println("1::tf1.myStaticFlag_ = " +  tf1.getFlag());17.     tf1.setFlag(true);18. 19.     TestFlag tf2 = new TestFlag();20.     System.out.println("2::tf1.myStaticFlag_ = " +  tf1.getFlag());21.     System.out.println("3::tf2.myStaticFlag_ = " +  tf2.getFlag());22.   }23. }

Running this program gives you the following output:


The class TestFlag defines one private variable, myStaticFlag, and two methods to set it or query its state. Lines 15-16 instantiate a new object of type TestFlag and print out the state of the flag. The state is initially false since it was declared as such. Line 17 sets the state of the flag to true for object tf1. Line 19 constructs another object, tf2, of type TestFlag. Lines 21-22 print out the value of the flag for both tf1 and tf2. Note that the flag retains its state of true and does not get initialized to false because it is a class variable. Hence, both tf1.myStaticFlag and tf2.myStaticFlag refer to the same variable.

For a demonstration of how static flags are typically used, see the Tips: “Implementing the Singleton Pattern” and “A Mutable Singleton.”

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may