Not Initialized
Another in the riddle me this segments. I came across this, it's not a big deal but I wonder about it. following are 2 classes, one compiles, one doesn't.
// This one compiles
publicclass testNon{
publicint test;
publicint test2;
public Integer test3;
public testNon(){
test = test;
test2 = 55 + test;
test3 =new Integer(test);
}
publicstaticvoid main(String args[]){
System.out.println("Start");
testNon T =new testNon();
System.out.println("Test = " + T.test);
System.out.println("Test2 = " + T.test2);
System.out.println("Test3 = " + T.test3.intValue());
System.out.println("End");
}
}
// This one doesn't
publicclass testNon{
publicint test;
publicint test2;
public Integer test3;
/** Creates new testNon */
public testNon(){
test = test;
test2 = 55 + test;
test3 =new Integer(test);
}
publicvoid getID(){
int I;
I += 2;
}
publicstaticvoid main(String args[]){
System.out.println("Start");
testNon T =new testNon();
System.out.println("Test = " + T.test);
System.out.println("Test2 = " + T.test2);
System.out.println("Test3 = " + T.test3.intValue());
System.out.println("End");
}
}
In the first class the test int is not set to anything but when the class is instatiated, the test int initializes to 0.
In the second class, trying to use an int in a method where the int has not been initialized won't compile. The error is
variable I might not have been initialized
Just curious. Why does the instantiation of the object auto initialize the int (and other primitives I assume) and the method doesn't?
Thanks for a great forum.

