1

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) transformer.create(message,sourceSystem,flowName);  
}

However it cannot be accessed outside of these?

validator.validate(object);

cannot be resolved to a variable. But surely the object is being created an assigned within the if-else statement. The object will always be created, therefore why is the compiler telling me it cannot be resolved to a variable. Yes I get local and global vairables however if I need to access a common method between UO and MO.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
Will
  • 8,246
  • 16
  • 60
  • 92

6 Answers6

6

You can't achieve what you want this way. Better try this:

Object object = transformer.create(message,sourceSystem,flowName);
if( object instanceof MO )
{
  //cast and use it as MO
}//if
else
{
  //cast and use it as UO
}//else

Inheritance could help you to group code of both blocks if UO and MO share the methods you use in each block.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • 1
    How can I check that this is an instanceof MO? I am not aware how to do this in Java nor does the Object class contain the method for this. – Will Aug 24 '11 at 14:05
  • 2
    ``instanceof`` is a java keyword and does exactly that. Just try it! – f1sh Aug 24 '11 at 14:12
0

You are actually declaring two variables named object. Each of them has a very limited scope, namely the block they are found in.

Assuming for a second that you could access the variables after the if statement completes: what would the type of object be in that case?

You need to declare a variable before the if and assign to that in both cases. You'll need to decide on a single type for this, however. In the worst case you need to use Object.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
0

Declare it outside the if blocks.

YourParentType object;
if(sourceSystem.equalsIgnoreCase("Src"))
    {
    object = (MO) transformer.create(message,sourceSystem,flowName);
    }
    else{
    object = (URO) transformer.create(message,sourceSystem,flowName);
    }
  • MO and URO are related objects, you you may want to have a common parent for these. This class is what I mean by YourParentType
  • What you are trying to do is similar to the Factory Design Pattern. You will find that this kind of ifs repeat in may places in your code. You can move them to a "Factory" class. (Or use the transformer as a factory by moving the if into it, depending on what it does.)
Nivas
  • 18,126
  • 4
  • 62
  • 76
0

Try this:

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

Then in follow-up code can be based on which one of these was not null.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

The variable object are only known within the blocks. If you want to use object in validator, then do it like this:

Object object = null;
if(sourceSystem.equalsIgnoreCase("Src")) {
  object = transformer.create(message,sourceSystem,flowName);
} else {  
  object = transformer.create(message,sourceSystem,flowName);  
}
validator.validate(object);

or even much shorter

validator.validate(transformer.create(message, sourceSystem, flowName));

Note that casting is not required (as long as create returns the type that is used by validate)

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
0

The most elegant way of doing this would be to declare an interface, (say MessageInterface) and have both your MO and URO objects implement it.

Then you'll have to do something like this:

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

If you don't have direct access to declaration of MO and URO you can still do this by creating a wrapper for them that implements the above interface.

Ashkan Aryan
  • 3,504
  • 4
  • 30
  • 44