public static void main(String args) { Field<String> f1 = new Field<String>("test"); Field<Integer> f2 = new Field<Integer>(1); Field<Long> f3 = new Field<Long>(1l);
List<Field<?>> fields = new ArrayList<Field<?>>(); fields.add(f1); fields.add(f2); fields.add(f3);
for (Field<?> field : fields) { Object value = field.getValue(); // Compilation error! Expects specific type. field.doSomething(value); } }
private static class Field<T> { private final T value;
public Field(T value) { super(); this.value = value; }
Is there any special syntax to cast the value passed in field.doSomething(value) to the respective generic type?
Note that the example I have given is just to describe my problem. In the actual scenario, the value is not obtained from Field object itself, I use the Field object to obtain the value from Message object and its not accessible locally.
> Without looking deep at all, the first glaring problem is > that there is no code defining the T class. > > I don't see anything like > > public class T{ > //T methods > }
That's not an issue.
publicclass A<T>{
}
Is perfectly legal, all its saying is that make sure you type it when referencing the class.
Haven't gone through the code either. Will take a look.