Newsletter sign-up
View all newsletters

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

Class and object initialization

Learn how to prepare classes and objects for use in an executing program

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
This article marks a milestone in our exploration of client-side Java. We now begin moving away from simpler Java language concepts toward more advanced theories -- and toward a grand tour of Java's standard class library. To begin our journey, this article focuses on initialization.

Initialization prepares classes and objects for use during a program's execution. Although we tend to think of initialization in terms of assigning values to variables, initialization is so much more. For example, initialization might involve opening a file and reading its contents into a memory buffer, registering a database driver, preparing a memory buffer to hold an image's contents, acquiring the resources necessary for playing a video, and so on. Think of anything that prepares a class or an object for use in a program as initialization.

Java supports initialization via language features collectively known as initializers. Because Java handles class initialization differently from object initialization, and because classes initialize before objects, we first explore class initialization and class-oriented initializers. Later, we explore object initialization and object-oriented initializers.

Class initialization

A program consists of classes. Before a Java application runs, Java's class loader loads its starting class -- the class with a public static void main(String [] args) method -- and Java's byte code verifier verifies the class. Then that class initializes. The simplest kind of class initialization is automatic initialization of class fields to default values. Listing 1 demonstrates that initialization:

Listing 1. ClassInitializationDemo1.java

// ClassInitializationDemo1.java
class ClassInitializationDemo1
{
   static boolean b;
   static byte by;
   static char c;
   static double d;
   static float f;
   static int i;
   static long l;
   static short s;
   static String st;
   public static void main (String [] args)
   {
      System.out.println ("b = " + b);
      System.out.println ("by = " + by);
      System.out.println ("c = " + c);
      System.out.println ("d = " + d);
      System.out.println ("f = " + f);
      System.out.println ("i = " + i);
      System.out.println ("l = " + l);
      System.out.println ("s = " + s);
      System.out.println ("st = " + st);
   }
}


ClassInitializationDemo1's static keyword introduces a variety of class fields. As you can see, no explicit values assign to any of those fields. And yet, when you run ClassInitializationDemo1, you see the following output:

b = false
by = 0
c =  
d = 0.0
f = 0.0
i = 0
l = 0
s = 0
st = null


The false, 0, 0.0, and null values are the type-oriented representations of default values. They represent the result of all bits automatically set to zero in each class field. And what automatically set those bits to zero? The JVM, after a class is verified. (Note: In the preceding output, you do not see a value beside c = because the JVM interprets c's default value as the nondisplayable null value.)

Class field initializers

After automatic initialization, the next simplest kind of class initialization is the explicit initialization of class fields to values. Each class field explicitly initializes to a value via a class field initializer. Listing 2 illustrates several class field initializers:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (5)
Login
Forgot your account info?

Thanks - good presentationBy Anonymous on December 28, 2009, 5:09 amSurfing to the reason of my initializing problem I get your article. After a long time of programming in java I need your article to understand the reason of missing...

Reply | Read entire comment

Enum initialization circularity?By Anonymous on December 2, 2009, 3:37 amJava lang spec says that: "It is a compile-time error to reference a static field of an enum type that is not a compile-time constant (§15.28) from constructors,...

Reply | Read entire comment

Very helpful article, thanxBy Anonymous on September 28, 2009, 7:56 pmThis question is risen by young curious java students. But there are low chances to describe without the deep concept understanding and seeing the whole picture....

Reply | Read entire comment

I'm always curious about it...By Anonymous on September 3, 2009, 2:59 amI'm always curious about it. You gave me a highway express. Thanks a lot.

Reply | Read entire comment

Very usefulBy Anonymous on May 3, 2009, 11:30 amThank you

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources