Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Object-oriented language basics, Part 2

Declare and access fields and methods

  • Print
  • Feedback

Page 5 of 7

Declare constants with the final keyword. Just as there are two kinds of fields, instance and class, constants come in two flavors -- instance and class. For efficiency, create class constants, or final static fields. Consider this example:

class Constants
{
   final int FIRST = 1;
   final static int SECOND = 2;
   public static void main (String [] args)
   {
      int iteration = SECOND;
      if (iteration == FIRST) // Compiler error.
          System.out.println ("first iteration");
      else
      if (iteration == SECOND)
          System.out.println ("second iteration");
   }
}


The above example's Constants class declares a pair of constants -- FIRST and SECOND. FIRST is an instance constant because the JVM creates a separate copy of FIRST for each Constants object. In contrast, because the JVM creates a single copy of SECOND after loading Constants, SECOND is a class constant.

Note: A compiler error occurs when you attempt to directly access FIRST in main(). Constant FIRST does not exist until an object is created, but then FIRST is only accessible to that object -- not the class.

Use constants for the following reasons:

  1. In source code, it is easier and less error-prone to change a constant's initial value than to replace all occurrences of a hard-coded magic number.
  2. Constants facilitate reading and understanding source code. For example, the constant NUM_ARRAY_ELEMENTS is more meaningful than the number 12.


Enumerated types

Suppose you write a Java program, Zoo1, that models a zoo of circus animals:

Listing 1. Zoo1.java

// Zoo1.java
class CircusAnimal
{
   final static int TIGER = 1;
   final static int LION = 2;
   final static int ELEPHANT = 3;
   final static int MONKEY = 4;
}
class Zoo1
{
   private int animal;
   public static void main (String [] args)
   {
      Zoo1 z1 = new Zoo1 ();
      z1.animal = CircusAnimal.TIGER;
      // Some time later ...
      if (z1.animal == CircusAnimal.TIGER)
          System.out.println ("This circus animal is a tiger!");
      else
      if (z1.animal == CircusAnimal.MONKEY)
          System.out.println ("This circus animal is a monkey!");
      else
          System.out.println ("Don't know what this circus animal is!");
   }
}


Zoo1.java declares a pair of classes: CircusAnimal and Zoo1. Class CircusAnimal declares some convenient integer constants that represent various circus animals, whereas Zoo1 is the starting class -- its main() method runs the Zoo1 application.

The main() method creates a Zoo1 object, assigns that object's reference to z1, and initializes the object's animal instance field to CircusAnimal.TIGER. Even though animal is private, method main() has access to that variable -- through the z1 object reference variable -- because main() is part of the same class in which animal is declared. Later, main() checks animal's current value and outputs an appropriate message based on its findings.

Zoo1 features a problem: animal's int data type keyword. Suppose we assign z1.animal to 978324 -- which is legal because both animal and 978324 have the integer data type. The resulting assignment would be meaningless. What animal does 978324 represent? Using an integer as animal's data type makes it possible to create irrational code, which often leads to bugs. A solution to that problem is to give animal a more appropriate data type and restrict the set of values that can assign to animal. That is the rationale behind using enumerated data types.

  • Print
  • Feedback

Resources