Category: 

What Are Static Variables?

Article Details
  • Written By: Jessica Susan Reuter
  • Edited By: Allegra J. Lingo
  • Last Modified Date: 03 December 2016
  • Copyright Protected:
    2003-2016
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
The mongoose was introduced to Hawaii in order to kill rats, but mongooses hunt in the day, while rats are nocturnal.  more...

December 7 ,  1941 :  Japanese bombers attack Pearl Harbor.  more...

Static variables, also called global variables, are pieces of data that are not affiliated with a particular instance of a class. Only one value for these variables can exist regardless of the number of class objects that are created. Depending on the context, static variables can be either more flexible or more restrictive than their instance variable counterparts, which have their own discrete values for every specific object of a class type. In object oriented programming languages, good programming practice usually dictates that the use of static objects, methods, or variables be kept to a minimum, but they do have useful applications.

One major reason that static variables are considered restrictive at times is because no more than one value can ever exist for a variable. Any assignment to the variable overwrites the previous one, and any information in the previous value is lost. Without multiple copies of the variable, multiple data values cannot be stored. If the value is changed, every object that it affects must function with the new value, and if the old value was not meant to be deleted, the change could prove detrimental to every instance object of a particular class type. Unless the variable is somehow locked at creation, the risk of unanticipated change and the subsequent havoc it could wreak on objects causes some programmers to avoid static variables whenever possible.

Ad

Static variables can sometimes be considered flexible for the same reason they may be called restrictive. Even if a variable's value isn't locked, planned changes to its value can have positive effects. A variable that is shared among all objects of a class is guaranteed to be consistent and can be used both inside and outside the class with confidence that its value is always the same. Variables that are static have a variety of useful applications, most notably in maintenance of constants and implementation of serialization. Java in particular relies heavily on this static serialization mechanism.

There are certain general rules which programmers often use while implementing static variables. These variables usually work best as small data values because large static objects can make a program far more rigid than it has to be. Small data values can be quickly and easily changed without too much fear of introducing errors. Large static objects take more time and effort to change and are also more prone to having an error introduced that would disrupt all class objects. These are guidelines, not unbreakable rules, and there may be uses for both small and large variables, depending on the program.

Ad

You might also Like

Recommended

Discuss this Article

Post your comments

Post Anonymously

Login

username
password
forgot password?

Register

username
password
confirm
email