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 6 of 7

An enumerated data type is a reference data type with a restricted set of values. Each value is an object created from the enumerated data type, as shown below:

Listing 2. Zoo2.java

// Zoo2.java
class CircusAnimal
{
   static final CircusAnimal TIGER = new CircusAnimal ("Tiger");
   static final CircusAnimal LION = new CircusAnimal ("Lion");
   static final CircusAnimal ELEPHANT = new CircusAnimal ("Elephant");
   static final CircusAnimal MONKEY = new CircusAnimal ("Monkey");
   private String animalName;
   private CircusAnimal (String name)
   {
      animalName = name;
   }
   public String toString ()
   {
      return animalName;
   }
}
class Zoo2
{
   private CircusAnimal animal;
   public static void main (String [] args)
   {
      Zoo2 z2 = new Zoo2 ();
      z2.animal = CircusAnimal.TIGER;
      // Some time later ...
      if (z2.animal == CircusAnimal.TIGER)
          System.out.println ("This circus animal is a tiger!");
      else
      if (z2.animal == CircusAnimal.MONKEY)
          System.out.println ("This circus animal is a monkey!");
      else
          System.out.println ("Don't know what this circus animal is!");
   }
}


The Zoo2 class closely resembles Listing 1's Zoo1 class, with the major difference being animal's data type. Instead of int, animal now has the CircusAnimal data type -- only references to objects created from CircusAnimal can be assigned to animal.

The CircusAnimal class declares four CircusAnimal constants: TIGER, LION, ELEPHANT, and MONKEY. Each constant initializes to a CircusAnimal object.

A special CircusAnimal constructor, which takes a single String argument, is declared. The string passed to the constructor is saved in a private animalName field. (I discuss constructors later in this article.)

The constructor is declared private to prevent the creation of CircusAnimal objects beyond those four objects declared as constants. You don't want someone to create a CircusAnimal object equivalent to the meaningless 978324. Only the four CircusAnimal constants are valid for the Zoo2 program.

Why is a toString() method declared in CircusAnimal? That method returns the value of the String object, whose reference is assigned to animalName. Suppose you call System.out.println (z2.animal) after initializing z2.animal to CircusAnimal.TIGER. The result: Tiger will print, which is the value passed to CircusAnimal's constructor when the TIGER constant was created. System.out.println() acquires Tiger behind the scenes by calling the toString() method. (I'll have more to say about toString() in a later article.)

Now that you know how to declare and access fields, you need to learn how to declare and access methods.

Declaring methods

Java uses the term method to refer to named bodies of code that are associated with classes. Whereas fields hold either an object's state or class values, methods describe the behaviors of either an object or a class. Furthermore, Java's methods are completely declared inside classes. To declare a method in source code, use the following Java syntax:

[ ( 'public' | 'private' | 'protected' ) ] 
  ( [ 'abstract' ] | [ 'final' ] [ 'static' ] [ 'native' ] [ 'synchronized' ] )
    return_data_type method_name '(' [ parameter_list  ] ')'
    compound_statement


A method declaration consists of a method signature followed by a compound statement. The method signature specifies the method's name, return data type, parameter list, access specifiers, modifiers, and the types of exceptions the method can throw. (I discuss throwing exceptions in a future article.) The compound statement is a block (group) of statements that executes when the method is called by code executing in another method. Code that calls a method is known as the method's caller. See "Non-Object-Oriented Language Basics, Part 3" for the syntax of a compound statement.

  • Print
  • Feedback

Resources