Classic Tutorials for Java Beginners

Object initialization in Java

The full story of object initialization in the Java language and virtual machine

1 2 3 4 Page 4
Page 4 of 4

The strict ordering of instance variable initialization enforced by the Java compiler is, in part, an effort to ensure that during the initialization process, instance variables are never used before they have been initialized to their proper initial values. As illustrated earlier in this article, however, the rules of ordering are not bulletproof. There are ways you can use an instance variable during initialization before it has been initialized to its proper value, while it still has its default value. In the case of instance variable initializers, you can invoke a method that uses a variable declared textually after the variable being initialized. Another way to use an instance variable before it has been properly initialized is to invoke a method from a superclass initializer or constructor that uses instance variables in a subclass.

Unlike C++, which treats the invocation of virtual functions from constructors specially, Java methods invoked from <init> methods behave the same as if they were invoked from any method. If <init> in a superclass invokes a method that has been overridden in a subclass, the subclass's implementation of that method will run. If the subclass's method implementation uses instance variables explicitly declared in the subclass, those variables will still have their default initial values.

You should be careful when you invoke methods from initializers or constructors, because you can end up using instance variables before they've been properly initialized -- while they still have their default initial values. It is fine to use variables while they still have their default initial values, so long as it is the result you are aiming for. If you invoke non-private methods from initializers and constructors, remember that later some other programmer could come along, extend your class, and override those methods, thereby thwarting your grand initialization scheme.

Conclusion

Java goes to great lengths to help you give newly-created objects a good start in life. Java's initialization mechanisms help you to ensure that objects you design begin their lives in a valid, predictable state. But these mechanisms do not force you to design objects in this way. In the end, if you want your programs to produce objects that always begin their lives in a proper state, you must use the initialization mechanisms correctly. For advice on how to put the initialization mechanisms described in this article to use in your programs and designs, see this month's Design Techniques column, "Designing object initialization."

Bill Venners has been writing software professionally for 12 years. Based in Silicon Valley, he provides software consulting and training services under the name Artima Software Company. Over the years he has developed software for the consumer electronics, education, semiconductor, and life insurance industries. He has programmed in many languages on many platforms: assembly language on various microprocessors, C on Unix, C++ on Windows, Java on the Web. He is author of the book: Inside the Java Virtual Machine, published by McGraw-Hill.

Learn more about this topic

  • For the source code listings that go with this article, see http://www.javaworld.com/jw-03-1998/initialization/initialization.zip
  • "Initialization of Fields," a section in the Java Language Specification, describes instance variable initializers http://www.javasoft.com/docs/books/jls/html/8.doc.html#24510
  • "Constructor Declarations," a section in the Java Language Specification, describes instance variable initializers http://www.javasoft.com/docs/books/jls/html/8.doc.html#41652
  • "Creation of New Class Instances," a section in the Java Virtual Machine Specification, gives a terse, technical description of the process of object initialization in the JVM http://www.javasoft.com/docs/books/vmspec/html/Concepts.doc.html#19124
1 2 3 4 Page 4
Page 4 of 4