statement « cast « Java Data Type Q&A

1. Cast element in Java For Each statement

Is it possible (or even advisable) to cast the element retrieved from a for each statement in the statement itself? I do know that each element in list will be of ...

2. Typecasting in Java with respect to if statement

Why does this code not work? Specifically, why does the typecast done outside of the if statement does not hold inside of it.

private SuperType currObj;

public void someMethod(SuperType currObj){


4. Casting during an if-else statement

I am trying to cast a object during an if - else statement as follows:

if(sourceSystem.equalsIgnoreCase("Src")) {
  MO object = (MO) transformer.create(message,sourceSystem,flowName);
} else {  
  UO object = (URO) ...