Although you will not be formally introduced to classes until chapter 9, it’s a good idea to show you the relationship between a class’s variables and constants and the main() method. A clear understanding of this relationship early in your Java programming career will save you a lot of headaches.
The main() method is a static method. Say this to yourself about a hundred times! “The main() method is a static method.” Because it is a static method it can only directly access class variables or constants that are declared to be static.
A variable or constant declaration, appearing in the body of a class definition outside of any method, declared to be static, is referred to as a class variable or constant, meaning the values they contain are available to all dynamically created objects of that class type.
A variable or constant declaration, appearing in the body of a class definition outside of any method, declared to be non-static, is referred to as an instance variable or constant, meaning there is a copy of each of those variables or constants for each dynamically created object. To access an instance variable or constant from a static method requires a reference to an object.
Example 6.14 gives the code for a class named TestClassThree. TestClassThree contains two static and two instance variable declarations.
Example 6.14: TestClassThree.java
1 public class TestClassThree { 2 public static int static_int_variable; // main() can access this variable 3 private static int static_int_variable_2; // and this one too... 4 5 public int instance_int_variable; // But it needs a reference to an object 6 private int instance_int_variable_2; // to access these instance variables... 7 8 public static void main(String[] args){ 9 System.out.println(static_int_variable); 10 System.out.println(static_int_variable_2); 11 System.out.println(TestClassThree.static_int_variable); 12 System.out.println(TestClassThree.static_int_variable_2); 13 14 TestClassThree tc3 = new TestClassThree(); 15 16 System.out.println(tc3.instance_int_variable); 17 System.out.println(tc3.instance_int_variable_2); 18 } 19 }
As you can see by examining the code, the main() method can directly access the static variables as is shown on lines 9 and 10. Lines 11 and 12 shows an alternative, fully-class-name-qualified, way of accessing TestClassThree’s static variables. You will see this access method used a lot in Java programs. Notice too that because the main() method belongs to the TestClassThree class, it can directly access private class variables as well.
However, for the static main() method to access the instance or non-static TestClassThree variables it must have a reference to a TestClassThree object. This is provided on line 14. The name of the reference variable is tc3 and is used to access each of the instance variables as is shown on lines 16 and 17.
Figure 6-14 shows the results of running example 6.14. Notice that all variable values are zero. This is the default value of integer type class and instance variables.
Figure 6-14: Results of Running Example 6.14