In this case, you do not use the foo variable after setting it, so it would be legal for the JVM to completely ignore the variable since it is never used and which will not change the result of your program.
However, this is unlikely to happen in debug mode.
In your case, foo should not get GC'ed as long as it is in scope, or you keep a link to it that includes a section after the try / catch block.
EDIT
Actually, I get the same behavior as in Netbeans 7.1.1 with Java 7.0_03 ...
One problem may be that since you are not setting the default value to foo
, you cannot use it after the try / catch block (it will not compile).
Bytcode
public static void main(java.lang.String[]); Code: 0: ldc #2
- Using
String foo = null;
as the first statement, in this case the debugger sees the value after the try / catch block:
public static void main(java.lang.String[]); Code: 0: aconst_null 1: astore_1 2: ldc #2
I am not a specialist in byte coding, but they are very similar to me ...
CONCLUSION
My personal conclusion is that for the debugger to show the value of foo
, it should run foo.toString()
some type that is not a valid statement after a catch block like foo
may not have been initialized. Adding System.out.println(foo)
in this section is not legal (does not compile). The debugger is a bit lost relative to the value and shows null
.
To convince yourself that this has nothing to do with the GC, you can try the following example:
public static void main(String[] args){ String foo; char[] c = null; try { foo = "bar"; c = foo.toCharArray(); int yoo = 5;
In the line foobar
you can see that c
contains bar
, but foo shows as null
. So, String still exists, but the debugger cannot show it.
An even funnier example:
public static void main(String[] args){ String foo; List<String> list = new ArrayList<String>(); try { foo = "bar"; list.add(foo); int yoo = 5;
On the foobar
line, foo
displayed as null
, but list
contains "bar"
... Nice.