public class Initialization {
// Member variable declarations can be uninitialized.
// If this is the case java gives them a default value,
// zero for type int
int memberInt;
// null for type String
String memberStr;
// Member variable declared and initialized.
String anotherStr = "hello world";
private void test() {
System.out.println("memberInt = " + memberInt);
System.out.println("memberStr = " + memberStr);
System.out.println("anotherStr = " + anotherStr);
// Local variables are not assigned default values
// and must be initialized, ie, assigned a value
// before they are used.
// So this uninitialized local variable
// is okay so far.
int localInt;
// As soon as I try to do something with localInt
// there will be a compile–time error.
//System.out.println("localInt = " + localInt);
}
public static void main(String[] args) {
new Initialization().test();
}
} |