I am loading an ArrayList from a save file which has a generic type inferred, but eclpise is telling me I haven’t checked the cast, why doesn’t (ArrayList< TypeOfObject >) count as a cast when (TypeOfObject) does?
game.evilSprites = (ArrayList<EvilSprite>) OIS.readObject();
casting the object as an evilSprite works…
game.evilSprite = (EvilSprite) OIS.readObject();
EDIT: great, thank you, so it is at least a valid cast, how do I check it to remove the warning?
It does count as a cast, but it can’t be checked properly. At execution time, an object has no information about whether it’s an
ArrayList<String>, anArrayList<Object>etc, due to type erasure. So your cast is checking that it’s anArrayListof some kind, but you could still get aClassCastExceptionwhen you try to fetch a sprite out of it – the values may not all beEvilSpritereferences.