An odd thought occurred to me. Since Java allows final variables to be set only once, then if I could initialize a variable to itself I could effectively have an uninitialized variable. Now, of course, the following does not work because the compiler appropriately complains "The blank final field TEST may not have been initialized":
public class Test
{
public static final int TEST;
static
{
TEST = TEST;
}
public static void main(String[] args)
{
System.out.println(Test.TEST);
}
}
But with a slight modification, I can get this to compile, and print the "uninitialized value" that I could not print before. (See Below) is this designed to be part of the language? Upon running this several times in a row interspersed with other programs, toggling swap on and off, etc., I've seen both the output "0" and "1" get printed from this function which tells me this truly is an uninitialized value of whatever happened to be floating around in memory there. Is this a bug?
public class Test
{
public static final int TEST;
static
{
// TEST = TEST;
TEST = initialValue();
}
public static int initialValue()
{
return Test.TEST;
}
public static void main(String[] args)
{
System.out.println(TEST);
}
}