Static Methods Part 2 Tutorial

In this tutorial, I will use a mix and match of static and non-static methods to complete the results of the all-important coin flipping study. Here is a quick refresher from my Introduction to the Static Modifier tutorial.
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.

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 ImportantStudy
C:\Java>cd ImportantStudy

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

import java.util.Random;

class ImportantStudy {
    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.BobsDay(); // yes! we can directly call a static method from a static method
        //Larry.LarrysDay(); // no! we cannot directly call a non-static method from a static method
        new Larry().LarrysDay(); // call by reference on non-static method
        new Mary().MarysDay();
        System.out.println("Drumroll ... and our results are: ");
        System.out.println("Heads total: " + RunningTotal.heads);
        System.out.println("Tails total: " + new RunningTotal().tails);
        System.out.println("I\'m calling it close enough for government work, let\'s grab a bite to eat. ");

class Bob {
	static void BobsDay() {
		Skills.workHard(300_000); // static method directly called from static method

class Larry {
	void LarrysDay() {
		Skills.workHard(350_000); // static method directly called from a non-static method

class Mary extends Skills {
	void MarysDay() {
		workHard(350_000); // static method directly called from a non-static method using inheritance

class Skills {

	static int flipCoin() {
		Random randomNumber = new Random();
		return randomNumber.nextInt(2); // the 2 argument will set the return range of either a 0 or 1.

	static void workHard(int numberOfFlips) {
		for (int i = 0; i < numberOfFlips; i++ ) {
			if (flipCoin() == 0)

class RunningTotal {
    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 ImportantStudy and press Enter.

C:\Java\ImportantStudy>java ImportantStudy // run this a few times for different results
Drumroll ... and our results are:
Heads total: 499972
Tails total: 500028
I'm calling it close enough for government work, let's grab a bite to eat..

Final thoughts

Learning how to make calls between static and non-static methods is a skill that will come in quite handy. Understanding that that there is exactly one copy of a class variable is critical, especially when you consider that a class variable can be accessed by a reference to the class it resides in. The static keyword applied to a variable is the only difference in syntax between an instance variable and a class variable.