something = ( (list.get(0) instanceof A? (A) : (B)list.get(0)) ) .getSomething(); Now it says A cannot be resolved. The problem seems to be that you can't have a cast that's not contiguous to the object you're casting: it doesn't have a problem with B, that is next to list.get(0), but it doesn't recognize A. The reason behind my choice of parenthesis ...