Class Variables and Class Methods

A class variable is one variable for a whole class, shared by all its objects. A hotel lends umbrellas to its guests. Each Umbrella object has its own color, while the count of loans, kept by adding 1 at each loan and subtracting 1 at each return, is one number for the whole hotel.
public class Umbrella
{
private static int lentOut = 0;
private String color;
public Umbrella(String c)
{
color = c;
}
public void lend()
{
lentOut++;
}
public void giveBack()
{
lentOut--;
}
public static int getLentOut()
{
return lentOut;
}
}
One copy, shared
Umbrella red = new Umbrella("red");
Umbrella blue = new Umbrella("blue");
red.lend();
blue.lend();
red.giveBack();
System.out.println(Umbrella.getLentOut());
Each object has its own color, but lentOut exists once. The two calls to lend raise it to 2, and giveBack lowers it to 1, so the segment prints 1. It doesn't matter which umbrella the calls go through, since every one of them changes the same variable.
The call Umbrella.getLentOut() names the class, not an object, because a class method needs no object. Since lentOut is private, code outside the class reads it through that method. If it were public, code in another class could write Umbrella.lentOut. The keywords static and private answer separate questions: how many copies there are, and who may use the name.
A value that can't change
The hotel's loan period is always 7 days, so it can be declared final, as in public static final int LOAN_DAYS = 7;. Then a statement like LOAN_DAYS = 10; doesn't compile. A final variable is a constant; static alone doesn't make a variable constant, and lentOut, which is static but not final, changes with every loan.
What a class method can use
A class method runs without any object, so it can use class variables and call other class methods, but it can't use an instance variable on its own. The method below doesn't compile, since there's no way to tell whose color it means.
public static String describe()
{
return color;
}
Given an object as a parameter, a class method can reach that object's data through it.
public static String describe(Umbrella u)
{
return u.color + ", " + lentOut + " lent out";
}
Here u.color is the color of the umbrella passed in, which the method can read because it belongs to Umbrella.