Below is the code snippet.
public class Operand<T> { private OperandStore store; private final int operandType, operandId; public Operand( int operandType, int operandId, OperandStore store ) { this.operandId = operandId; this.operandType = operandType; this.store = store; } @SuppressWarnings( "unchecked" ) public T evaluate() { try { return ( T ) store.getValue( operandType, operandId ); } catch ( Exception e ) { return null; } } } My getValue method:
public Object getValue(int operandType, int operandId ) { // return some value from a <String, Object> hashmap based on some condition } When I create a object of the above class, like this:
Operand<Integer> obj = new Operand<>(1,1,store); ... and make sure that store.getValue( operandType, operandId ) returns a string, I expect an error to occur in try block which is not happening. It's returning the string value instead.
Any reason? Kindly explain. Thanks.
OperandStore.getValue()method? In fact the class declaration too.HashMapin OperandStore? How did you actually create theOperandStore? It would be helpful, if you can post the class definition, with all the relevant methods being used here.OperandStoreclass is implemented.