Specifications
Sun Services
Java™ Programming Language
Module 4, slide 8 of 31
Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F
Initialization Before Use Principle
The compiler will verify that local variables have been
initialized before used.
javac TestInitBeforeUse.java
TestInitBeforeUse.java:10: variable y might not have been initialized
z = y + x; // Possible use before initialization
^
1 error
3 public void doComputation() {
4 int x = (int)(Math.random() * 100);
5 int y;
6 int z;
7 if (x > 50) {
8 y = 9;
9 }
10 z = y + x; // Possible use before initialization
11 }










