Last updated 2004-06-28 by Roedy
Green ©1996-2004 Canadian Mind Products
Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
You are here : home : Java Glossary : I words : initialisation.
There are three kinds of variable, local, instance (one member per object), and static (one per class). When you first use a class, its class variables are initialised. When you first use a new SomeConstructor, the object's instance variables are initialised. There is no automatic initialisation for local variables. Usually you initialise them inline. Here is the order in which the six types of initialisation are applied. The types of initialisation near the top of the table are applied first.
| Type of Initialisation | Local | Instance | Static | Notes |
|---|---|---|---|---|
| automatic null or zero | No | Yes | Yes | No code needed. char = 0, not ' '; reference = null, int = 0; |
| inline known finals
e.g. final int MAX_WEIGHT = 1000; |
Yes | Yes | Yes | If the compiler can figure out the value of the expression at compile time, it gets done first ahead of the other inline initialisations. If it can't, then it is treated as an ordinary inline initialisation, even though it is final. |
| inline
e.g. int a = 1; |
Yes | Yes | Yes | Interleaved in textual order with static {} or instance {}. |
| static init block
e.g. static {a = 1;} |
Yes* | No | Yes | Interleaved in textual order with inline initialisations. |
| init block
e.g. {a = 1;} |
Yes* | Yes | Yes | Interleaved in textual order with inline initialisations. |
| constructor
e.g. a = 1; |
Yes | Yes | Yes | constructor to see how constructors are chained to do a series of initialisations |
| assignment in a method
e.g. a = 1; |
Yes | Yes | Yes | procedural code, not really initialisation. |
According the the Java language specification, after the zero initialisations, the class variable initializers and static initializers of the class, or the field initializers of the interface, are executed in textual order, as though they were a single block, except that final class variables and fields of interfaces whose values are compile-time constants are initialized first. In other words static final constants known at compile time are logically done first.
Beware of depending on inline initialisations of member or class variables being done in the order the variables are listed because various beautifier tools such as VAJ may reoreder them. When the value of one variable depends on another, put the dependent initialisations in an init or static init block, where you have explicit control of the ordering, rather than using inline initialisations. The compiler is not smart like a spreadsheet to figure out the dependencies and calculate them in natural order. The constructor must call the constructor of the superclass as the very first thing it does. If you leave out this code, the compiler inserts it for you. This means when you invoke a constructor, first the base fields are initialised, then the subclass. You are invoking a daisy chain of constructors of the ancestor classes when you invoke a constructor.
home |
Canadian Mind Products | |||
| mindprod.com IP:[24.87.56.253] | ||||
| Your IP:[80.134.30.163] | ||||
| You are visitor number 2067. | ||||
| Please send errors, omissions and suggestions | ||||
| to improve this page to Roedy Green. | ||||
| You can get a fresh copy of this page from: | or possibly from your local J: drive mirror: | |||
| http://mindprod.com/jgloss/initialisation.html | J:\mindprod\jgloss\initialisation.html | |||