Fifth International World Wide Web Conference
Initialization
- No variables in Java have undefined state
- Arithmetic variables initialized to 0
- Boolean variables initialized to
false
- Char variables initialized to the null character '\u0000'
- Array/class references initialized to
null
- Use constructor method to provide explicit initial values
- Has same name as class name
- May have zero or more arguments
- May be overloaded to allow different initialization types
- Constructor is called when an object is allocated (via new)
- Default constructor (one with no arguments) called if no
arguments supplied to
new
- Otherwise, appropriate constructor called based on type and number of
arguments
Java - An Introductory Language Tutorial
(E.A.Johnson)