Incremental Java
Static Methods

public static void main

The four dreaded words for a Java instructor is public static void main. You have to spend a great deal of time learning what these terms mean. You should already know three of the four. The only word you may not know is static.

Static

All the methods you've seen so far (except main()) can be called instance methods. Instance methods are methods that are called on objects.

A static method (also called a class method) does not refer to any object at all.

Let's compare the two. Suppose you call a method getHeight(). This method call appears to have no arguments. However, there is an implicit argument: the object that the method is called on.

When you call a static method, there is no object to refer to. If there is no object, do you think there are instance variables? No, there isn't.

There is a class called Math where every method is static. Let's look at some code:

  double x = -3.14 ;
  double y = Math.abs( x ) ;
To call a static method, you give the name of the class (in this case, Math), then a dot, then the name of the static method, then any arguments.

I should point out that Math is not an object. You need to provide the class name because some other class might have an abs() method, and Java needs to know which abs() you're referring to.

Static and Non-Static Methods

A class can have both static and non-static (i.e., object) methods. When an object method is called, you need to have an object (e.g., rect.getHeight() where rect is an object).

Suppose Rectangle has a static method, such as public static double getGoldenEdge( double side ), where you provide the length of one side, and it computes the length of the longer side to preserve the golden ratio (a "pleasing" ratio for rectangles).

You can call this static method as Rectangle.getGoldenEdge( x ). When this method is called, the method definition can't include any instance variables. After all, what instance variables should it refer to? Rectangle is not an object!

main()

It makes sense for main() to be static. When you run a program from the prompt.
  java Foo
This runs the main() method of Foo. No objects are created when you start running main(). So it should be static. And it is.

Static Variables

Java also supports static variables. You put them in the class just like instance variables, but you put the keyword static in front.
public class Foo {
   private static int cost = 10 ;
   private static int tax = 7 ;
}
Every object has a copy of instance variables. For example, every object has a width and height variable.

However, there is only one static variable (per declaration), no matter how many objects exist. There may be no objects at all, yet the static variables exist. For example, in the example above, there is one cost variable and one tax variable (in the Foo class---other classes may have similarly named static variables).

All objects can access these two variables, but this means they are shared. If foo1, a Foo object, changes cost to 11, then foo2, another Foo object sees this new value as 11. You don't even need an object to change these values (if the static variables are public, that is).

For example, you could modify the static variables as:

   Foo.cost = 11 ;
In the example above, we made these static variables private, so we would need static methods or object method to modify the values.

No Constructors

Because static variables do not exist in any object (although, objects can access these variables from the class itself), there are no constructors used to initialize static variables.

We only have one way to initialize static variables. Use initializers as shown in the partial class definition above. Initializers for static variables are essentially called when the program runs (well, more like when the variables are used for the first time).

Why Static Variables?

Static variables aren't used that much. The most common use is for a design pattern called Singleton or Typesafe Enum. We'll talk about design patterns later on, as it's an advanced topic. These patterns are useful, and have increased the usefulness of static variables. Even so, most variables are instance variables.

Belong to the Class

Instance variables appear in each object. Static variables belong to the class. The class has one copy. Any object can access the static variables. They can also be accessed through static methods. If the variables are public, they can be accessed anywhere.

Static variables are useful to keep information that is common to every object. This might be the status of something. For example, maybe one could determine if there is a discount by checking a boolean static variable. There's no need for every object to hold this information. Just one copy is enough. This can be made into a static variable.