If a class declaration includes a field, then every object that is of this class will include this field. Such a field is called an instance variable. It is also possible to have a field that is associated with the class rather than with each object of the class. Such a field is called a class variable, and it is indicated by using a static modifier.
A superficial example would be a class declaration that has a field that is used to count how many times methods of the class have been called:
private static int tNumberOfCalls = 0;In order for this to work, we would need to add the statement:
tNumberOfCalls++:to each of the methods of the class. Here is such a class declaration:
0344: // A class for representing values that are dates.
0345: // Barry Cornelius, 20th September 1999
0346: import java.util. StringTokenizer;
0347: public class Date
0348: {
0349: private static int iNumberOfCalls = 0;
0350: private int iYear;
0351: private int iMonth;
0352: private int iDay;
0353: public static int getNumberOfCalls()
0354: {
0355: return iNumberOfCalls;
0356: }
0357: public Date(final int pYear, final int pMonth, final int pDay)
0358: {
0359: iNumberOfCalls++;
0360: iYear = pYear;
0361: iMonth = pMonth;
0362: iDay = pDay;
0363: }
0364: public int getYear()
0365: {
0366: iNumberOfCalls++;
0367: return iYear;
0368: }
0369: ...
0370: public void setYear(final int pYear)
0371: {
0372: iNumberOfCalls++;
0373: iYear = pYear;
0374: }
0375: ...
0376: public String toString()
0377: {
0378: iNumberOfCalls++;
0379: return iYear + "-" + iMonth/10 + iMonth%10 + "-" + iDay/10 + iDay%10;
0380: }
0381: }
A method that can be applied to any object of its class is called an instance method. It is also possible for a class to have standalone methods: such a method is called a class method.
So we could add to the Date declaration a method that returns the value of the class variable iNumberOfCalls, i.e., that returns the number of times methods of the class have been called. Such a method declaration appears in the above class declaration. Note that it is easy to detect a class method because its declaration includes static.
A class method is called by putting the name of the class on the left of the dot. An example of the call of getNumberOfCalls() is shown in the following version of the NoelProg program. It outputs the value 7:
0382: // This program creates objects of the version of the // NoelProg.java
0383: // class Date that keeps track of the number of calls of its methods.
0384: public class NoelProg
0385: {
0386: public static void main(final String[] pArgs)
0387: {
0388: final Date tNoelDate = new Date(1999, 12, 25); // 1
0389: final int tDay = tNoelDate.getDay(); // 2
0390: tNoelDate.setDay(tDay + 1); // 3
0391: System.out.println(tNoelDate.toString()); // 4
0392: System.out.println(tNoelDate); // 5
0393: final Date tAnotherDate = new Date(2000, 12, 25); // 6
0394: System.out.println(tAnotherDate); // 7
0395: System.out.println("number of calls is: " + Date.getNumberOfCalls());
0396: }
0397: }
0398:
As a class method is not applied to an instance of a class, it does not make sense to refer to non-static members (e.g., iDay and toString) in the block of the method of a class method (e.g., getNumberOfCalls). Any attempt to do this produces a compilation error like Can't make a static reference to nonstatic variable iDay in class Date.
If it is appropriate for a class to have a constant associated with it, then you can use a class variable whose declaration includes the final modifier. For example, the class java.lang.Math includes:
public static final double PI = 3.14159265358979323846;