2009-06-19

Puzzling behaviour

In the spirit of Java Puzzlers, what does this program print (if anything)?

public class java
{
static lang lang = new lang();

static class lang
{
static Long Short = new Long();

static class Short
{
}

static class Long
extends Short
{
}
}

public static void main(String[] args) {
System.out.println(java.lang.Short.class.getSimpleName());
}
}

2 comments:

Anonymous said...

is that so puzzling ?

if you change it to

public static void main(String[] args) {
System.out.println(java.lang.Short.getClass().getSimpleName());
}

then you get "Long".

espenhw said...

The puzzling part is, of course, that the .class operator resolves java.lang.Short as the class, not the field.