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.
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!
java FooThis runs the main() method of Foo. No objects are created when you start running main(). So it should be static. And it is.
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.
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).
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.