The following java code works fine.
public static void main(String[] arg){
JPanel p = (new JPanel());
p.add( new Object(){
JButton f(JButton x){
x.setEnabled(false);
return x;
}}.f(new JButton("B")) );
JFrame w = new JFrame("W");
w.add(p);
w.pack();
w.setVisible(true);
}
If the method is changed to its generic form the program fails.
public static void main(String[] arg){
JPanel p = (new JPanel());
p.add( new Object(){
<T> T f(T x){
x.setEnabled(false);
return x;
}}.f(new JButton("B")) );
JFrame w = new JFrame("W");
w.add(p);
w.pack();
w.setVisible(true);
}
Why does it fail? How can you define generic methods within an anonymous class?
This question is for learning purposes.