I am new to using the new CLASS_NAME () {STUFF} Java function, but what I met seems strange. Consider the following code:
class Test
{
public String a;
public static void main( String[] args ) throws java.lang.Exception
{
String j = "abc";
final String s = j;
Test v = new Test();
v.a = s;
Test e = new Test() {
public String a = s;
};
Test g = new Test();
g.a = s;
System.out.println( v.a );
System.out.println( e.a );
System.out.println( g.a );
}
}
I think the output of this program will be:
abc
abc
abc
Instead it is:
abc
null
abc
I am really confused why this is so. I taught myself this function myself, so I really know little about it. Any help is appreciated. Thanks!
source
share