The Artima Developer Community
Sponsored Link

Designing Fields and Methods
How to Keep Fields Focused and Methods Decoupled
by Bill Venners
First Published in JavaWorld, March 1998

<<  Page 2 of 7  >>


Using constants
Another rule of thumb to follow when creating fields is to use constants (static final variables) for constant values that are passed to, returned from, or used within methods. If a method expects one of a finite set of constant values in one of its parameters, defining constants helps make it more obvious to client programmers what needs to be passed in that parameter. Likewise, if a method returns one of a finite set of values, declaring constants makes it more obvious to client programmers what to expect as output. For example, it is easier to understand this:

if (cup.getSize() == CoffeeCup.TALL) {

than it is to understand this:

if (cup.getSize() == 1) {

You should also define constants for internal use by the methods of a class -- even if those constants aren't used outside the class -- so they are easier to understand and change. Using constants makes code more flexible. If you realize you miscalculated a value and you didn't use a constant, you'll have to go through your code and change every occurrence of the hard-coded value. If you did use a constant, however, you'll only need to change it where it is defined as a constant.

Constants and the Java compiler
A useful thing to know about the Java compiler is that it treats static final fields (constants) differently than other kinds of fields. References to static final variables initialized to a compile-time constant are resolved at compile-time to a local copy of the constant value. This is true for constants of all the primitive types and of type java.lang.String.

Normally, when your class refers to another class -- say, class java.lang.Math -- the Java compiler places symbolic references to class Math into the class file for your class. For example, if a method of your class invokes Math.sin(), your class file will contain two symbolic references to Math:

To execute the code contained in your class that refers to Math.sin(), the JVM would need to load class Math to resolve the symbolic references.

If, on the other hand, your code only referred to the static final class variable PI declared in class Math, the Java compiler would not place any symbolic reference to Math in the class file for your class. Instead, it would simply place a copy of the literal value of Math.PI into your class's class file. To execute the code contained in your class that uses the Math.PI constant, the JVM would not need to load class Math.

The upshot of this feature of the Java compiler is that the JVM doesn't have to work any harder to use constants than it does to use literals. Preferring constants over literals is one of the few design guidelines that enhances program flexibility without risking any degradation of program performance.

<<  Page 2 of 7  >>

Sponsored Links

Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use