Introduction to the Static Modifier

The static keyword can be applied to variables, methods, blocks, and nested classes. Don't worry about blocks and nested classes yet, I will cover them in future tutorials. In a nutshell, when the static modifier is applied to a member, that member is shared between all objects. In addition, there is no need to create an instance of a class to access a static member, but you can if you want to. The static modifier has many purposes, so the best way to begin explaining what static does is to apply it to a simple variable and use a hypothetical example.
Suppose we just won a multi-million dollar grant for a really important government study to determine if a there really is a 50/50 chance that a coin will land on either heads or tails. The specs for the grant require a minimum of 1,000,000 flips of a coin to be performed. The best approach will be to hire a bunch of people to flip coins and press either a heads button or tails button depending on the result of the flip. We'll need a high-tech workstation (table, chair, coin, heads button, tails button) for every worker that will connect to a central computer that will tally two competing values – heads and tails.
The key to our brilliant new technology will be the static keyword applied to an instance variable. But wait! I just stated that a static member is shared between objects and we know that an instance variable is unique to an object. Well, by applying the static keyword to an instance variable it becomes a class variable that is shared between all objects. Enough said, let's put up a help wanted ad on craigslist and write some code.

Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>md StaticIntro
C:\Java>cd StaticIntro

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.

class StaticIntro {
    public static void main(String args[]) {
        // only three people answered my craigslist ad: Bob, Mary and Larry.
        // They each put in a hard day at the office with the following results:
        // Bob's work
        FlipIt.heads++; // directly access the class variable

        // Larry loves objects and refuses to embrace direct access
        new FlipIt().heads++; // access the class variable using an instance
        new FlipIt().tails++;
        new FlipIt().heads++;
        new FlipIt().tails++;

        // Mary's work

        System.out.println("Our grand total for our first day is ...");
        System.out.println("Heads total: " + FlipIt.heads);
        System.out.println("Tails total: " + new FlipIt().tails);

class FlipIt {
    static int heads = 0;
    static int tails = 0;

Now switch back to the command prompt (CMD) and type in javac and press Enter.
Now type in java StaticIntro and press Enter.

C:\Java\StaticIntro>java StaticIntro
Our grand total for our first day is ...
Heads total: 7
Tails total: 8

Final thoughts

The subject of the static keyword, is actually quite extensive. I will be covering various rules and ways to use the static keyword in future tutorials.