# Type inference puzzler

August 31, 2010

Does this compile?

public class Puzzle {
static Object one() {
return 1;
}
static <T> T safeOne() {
return (T) one();
}
public static void main(String[] args) {
int one = safeOne();
System.out.println(one);
}
}

Take a look at it carefully and think before answering. Why or why not? Once you’ve thought it through, give it a try.

Were you surprised by the result? Now try this: if you tried it out in Eclipse, try it with Sun’s javac. If you tried it with javac, try it with Eclipse.