Java Variables Example 2 –Class Variables

Graphic shows class variable in relationship to instance.
Class variables are the same across all instances of a class. They can be changed from anywhere, unless final is invoked.

Unlike instance variables, class variables are the same across the class and all instances of the class. They can be set from anywhere, but they change across the board in all instances.

Class Variables (Static Fields) A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change. Orcacle Java TutorialsVariables

Let’s look at our Bicyle class from my Java Variable Example 1,  this time we’ve added in a static field called numGears, following Oracles tutorial.

In our other example,  our application used this class as a framework for different brand bicycles.  For this example our main method is going to use the class to instantiate bikes for different people. This is a good example of the portability of classes, the same class being used in two different applications.

This code instantiates stevensBike and laurasBike, then prints the number of gears assigned to numGears assigned to each instance.

Here the output will be:

Steven’s bike has 6 gears.

Laura’s bike has 6 gears.

We didn’t have to set anything, the two instances inherited the static value from Bicycle. As Oracle puts it, “[s]ome times, you want to have a variable that is common to all objects.”

As noted in that same article, you don’t have to create a getter method to access the static field. A class variable will automatically return itself.  This is not conventional though. If you do that, it is not immediately evident that it is a static variable, because it looks like a method. Java syntax and conventions usually lean toward being obvious and conspicuous.

Note: As a developer who came to learn Java later in my programming life, many others like me complain that Java is verbose. There’s just too much there. (Like one of my blog posts.)  I find this annoying sometimes too. However, most of the time, file structure, syntax highlighting and IDE assistance make it painless.  With all these extra words, you may as well follow conventions and be obvious, explicit, and conspicuous.

Some other items to keep in mind:

  • Static variables are generally best used when you only need one copy
    • like a counter or a default.
  • They take up less memory, which may impact large datasets
  • It can be changed from any of the other instances. Unless the keyword “final” is used

Written By StevenLacks