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());
}
}

3 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.

Data Science Course in Bangalore said...

I was very happy to find this site. I really enjoyed reading this article today and think it might be one of the best articles I have read so far. I wanted to thank you for this excellent reading !! I really enjoy every part and have bookmarked you to see the new things you post. Well done for this excellent article. Please keep this work of the same quality.
Data Science Course in Bangalore