Next: if statement. Up: Class variables and if statements. Previous: None.
Textbook: Sections 7.5 and 2.9
Recall Java's scoping rule for variables: The variable is accessible everywhere within the set of braces where it was declared. This means that any variables declared within a particular method aren't available in other methods.
But what if you want a variable to be shared across methods? Such a beast would be called a global variable, since it's available globally. (This contrasts it to the variables declared within methods, which are called local variables.)
Java's class variable construct allows you to create global variables. To declare a class variable, you declare it on the same level where you put the methods, using the words public static.
With these class variable definitions, all the methods in the MinMax class can access the class variables min and max.import csbsju.cs160.*; public class MinMax { public static int min; // notice the addition of the words public static int max; // ``public static!'' // and so on
As an example where you might want to use this (though the example is quite contrived...) suppose you want to write a method to find both the minimum and the maximum of two numbers. The problem is that a method can return only a single value, and we want to return two values. We can accomplish this by using the class variables to hold the values.
This example is a little silly, since we might as well write separate min() and max() methods (or, even better, use the Math.min() and Math.max() methods already included with Java). But you can imagine we might have some more complex computation that ends up modifying several values.import csbsju.cs160.*; public class MinMax { public static int min; public static int max; public static void findMinMax(int a, int b) { if(a < b) { min = a; max = b; } else { min = b; max = a; } } public static void run() { int input1 = IO.readInt(); int input2 = IO.readInt(); findMinMax(input1, input2); IO.print("min: "); IO.println(min); IO.print("max: "); IO.println(max); } }
Note: Global variables are considered very poor form, and they should be avoided at almost all costs. The reason is that they hinder the understandability of the program considerably: Say somebody (like your professor, or your boss) opens up your program to try to figure out how it works. The first thing they'll have to do is figure out how the global variables work. Since global variables can change anywhere, the person will have to review the entire program to figure it out.
Generally, you should avoid them through the use of parameters and return values wherever possible. By the end of this class, we'll have seen enough Java that we'll be able to avoid them consistently. In practice, I've written tens of thousands of lines of Java code, and I would be able to count the number of global variables I've used on a single hand. They're just not a good idea; experienced programmers know this, and avoid them assiduously.
Sometimes you have a constant value you want to refer to, that you know the program will never change. These are constants, and Java provides a way to designate a class variable as a constant using the final keyword. You could place the following among your member definitions.
You know the number of feet in a mile isn't going to change while the program is running, so you might as well declare it to be final. (The standard convention for naming constants in Java is to use all capital letters, as here.)public static final int FEET_PER_MILE = 5280;
Here's a complete program that uses this.
import csbsju.cs160.*; public class Convert { public static final int FEET_PER_MILE = 5280; public static void compute(double feet) { IO.println(feet / FEET_PER_MILE); } }
Experienced programmers consider regular, non-final class variables as things to avoid, but they consider constants to be very good for esveral reasons.
Java has several built-in constants. For example, Math.PI represents the value of pi (3.14159...).
Next: if statement. Up: Class variables and if statements. Previous: None.