Accepted answer

i'd say it's the bug in the sun compiler reported here and here, because if you change your line to the one below it works with both, which seems to be exactly what is described in the bug reports.

return genericstest2.<t>readobject(new objectinputstream(new fileinputstream(file)));


if you can modify your method readobject to work transparently when called, you may also use following:

public static <t> t readobject(string file, class<t> type) throws exception {
    return type.cast(readobject(new objectinputstream(new fileinputstream(file))));

this way, caller is forced to specify the type of the result and compiler knows how to cast the result.


i found this issue in java version "1.6.0_22". it disappeared when i upgraded to java version "1.6.0_32" as it was fixed in update 25.


oracle jdk6 u22 should be correct but i've this problem with jdk6 u24 too

this is a bug of eclipse bug 98379.

this was not corrected but the problem is resolved via workaround like example in eclipse bugs (see link)


in this case, i'd say your code is wrong (and the sun compiler is right). there is nothing in your input arguments to readobject to actually infer the type t. in that case, you're better off to let it return object, and let clients manually cast the result type.

this should work (though i haven't tested it):

public static <t> t readobject(string file) throws exception {
    return genericstest2.<t>readobject(new objectinputstream(new fileinputstream(file)));

Related Query

More Query from same tag