Study guide: Class and object initialization

Brush up on Java terms, learn tips and cautions, review homework assignments, and read Jeff's answers to student questions

Glossary of terms

class block initializer
An initializer that makes complex class initialization possible. A class block initializer consists of the static keyword, an open brace character, initialization code, and a close brace character.
class field initializer
An initializer that makes simple class field initialization possible. A class field initializer combines an expression with an assignment operator, which evaluates the expression and assigns its result to a class field after a class loads and before any of its methods execute.
object block initializer
An initializer that makes complex object initialization possible. An object block initializer consists of an open brace character, initialization code, and a close brace character.
object field initializer
An initializer that makes simple object field initialization possible. An object field initializer combines an expression with an assignment operator, which causes the expression to evaluate and its result to assign to an object field during the constructor call portion of an object's creation.

Tips and cautions

These tips and cautions will help you write better programs and save you from agonizing over error messages produced by the compiler.

Tips

  • If you are interested in learning more about class and object initialization at the JVM level, investigate the SDK's javap program. That program generates a disassembly of Java class files.

Cautions

  • Avoid accessing subclass fields from superclass initializers. During superclass initialization, subclass fields have not initialized and only contain default values.
  • Avoid introducing constructors that call each other. Calling either constructor results in a stack overflow and does not execute object initializers.

Miscellaneous notes and thoughts

Java's standard class library contains many examples of class block initializers. In addition to Java Database Connectivity's (JDBC) use of that feature, Object uses a class block initializer to register its native methods (such as clone(), getClass(), and hashCode()) with the JVM. The following Object code fragment takes care of that task:

private static native void registerNatives ();
static
{
   registerNatives ();
}

And what is a native method? Keep reading Java 101 to find out.

Homework

Trace the initialization process through the Employees source code below. You should end up with a numbered list that includes all initialization steps until the last Accountant object finishes initializing.

Employees.java

// Employees.java
class Employee
{
   private String name;
   private double salary;
   static int count;
   static double bonus;
   Employee (String name, double salary)
   {
      this.name = name;
      this.salary = salary;
      if (this instanceof Accountant)
          this.salary += bonus;
   }
   static
   {
      // Pretend to load bonus from a database.
      bonus = 500.0;
   }
   {
      if (count > 5)
          bonus = 0.0;
      count++;
   }
   String getName ()
   {
      return name;
   }
   double getSalary ()
   {
      return salary;
   }
}
class Accountant extends Employee
{
   Accountant (String name, double salary)
   {
      super (name, salary);
   }
}
class Employees
{
   public static void main (String [] args)
   {
      String [] names =
      {
         "John Doe",
         "Jane Smith",
         "Jack Jones",
         "Bob Smyth",
         "Alice Doe",
         "Janet Jones"
      };
      double [] salaries =
      {
         40000.0,
         50000.0,
         30000.0,
         37500.0,
         52000.0,
         47000.0
      };
      for (int i = 0; i < names.length; i++)
           if (i < 3)
           {
               Employee e = new Employee (names [i], salaries [i]);
               System.out.println ("Name = " + e.getName ());
               System.out.println ("Salary = " + e.getSalary ());
           }
           else
           {
               Accountant a = new Accountant (names [i], salaries [i]);
               System.out.println ("Name = " + a.getName ());
               System.out.println ("Salary = " + a.getSalary ());
           }
   }
}

Answers to the Java 101 Challenge

Last month, I issued a challenge quiz as a way of reviewing material presented in the "Object-Oriented Language Basics" series. The following three entrants emerged as the winners:

  • Jason Davies
  • David Hibbs
  • Gagan Indus

Jason, David, and Gagan were the first three challenge contestants to answer all questions correctly. Congratulations to the three of you! You will each receive a JavaWorld sweatshirt. To those of you who entered the contest but did not win, my heartfelt thanks. You are also winners.

Below you will find the answers to the Java 101 Challenge. Each answer is shown in red, and I include a forward slash character in those fill-in-the-blank answers that indicate a choice between multiple possibilities.

Related:
1 2 Page 1
Page 1 of 2