I have the follwoing
test code:
***************************
package dir1;
import dir2.*;
import dir3.*;
public class Class1 {
public static void main(
String[] args) {
String id = "abc";
Class4.NUMBER = id;
Class2 class2 = new Class2();
class2.doIt();
}
}
**********************************
package dir2;
import dir3.*;
public class Class2 {
public void doIt() {
Class3 class3 = new Class3();
class3.doIt();
}
}
***************************************
package dir3;
public class Class3 {
public void doIt() {
Class4 class4 = new Class4();
class4.getIt();
}
}
********************************************
package dir3;
public class Class4 {
public static String NUMBER;
public void getIt() {
String newNumber = NUMBER + "def";
System.out.println("newNumber = " + newNumber);
}
}
**************************************
The output is "newNumber = abcdef".
Question: When Class4 instance access its method, is that static NUMBER variable still existing ?
If the Class1,2,3,4 are very long code, could the Class4 have been garbage collected when the program reaches the line of
String newNumber = NUMBER + "def";
in Class4 so that NUMBER is no longer "abc" ?
Is the above code guaranteed to be safe in terms of letting Class4 get the value of "abc" for variable NUMBER ?
Thanks,
Mike