Create an istance into if control.

P

palmis

I have a problem:

I want to create an instance of RimsAcyclic if ASSET=="Rims_A" or an
instance of Message if ASSET=="Rims_B". This objects have to have the
same name (message_temp).

This is my code:

if (ASSET=="Rims_A"){
RimsAcyclic message_temp = new RimsAcyclic();
}
else if (ASSET=="Rims_B"){
Message message_temp = new Message();
}

out of 'if control', message_temp is unknown.
How can I do?

Thanks
Palmis.
 
R

Robert Klemme

palmis said:
I have a problem:

I want to create an instance of RimsAcyclic if ASSET=="Rims_A" or an
instance of Message if ASSET=="Rims_B". This objects have to have the
same name (message_temp).

This is my code:

if (ASSET=="Rims_A"){
RimsAcyclic message_temp = new RimsAcyclic();
}
else if (ASSET=="Rims_B"){
Message message_temp = new Message();
}

out of 'if control', message_temp is unknown.
How can I do?

Thanks
Palmis.

Declare the variable prior to the if statement without intialization.
That way the compiler will catch any omissions you do.

Or use the ternary operator ?: instead of if-else.

And don't use == for String comparisons.

robert
 
Z

zero

Declare the variable prior to the if statement without intialization.
That way the compiler will catch any omissions you do.

Or use the ternary operator ?: instead of if-else.

And don't use == for String comparisons.

robert

Note that if you want the same name (why?), you can only declare
message_temp (why the underscore?, naming conventions in Java suggest
messageTem or even better tempMessage) as a superclass of one or both.
If RimsAcyclic is a superclass of Message, use

RimsAcyclic message_temp;

If Message is a superclass of RimsAcyclic, use

Message message_temp;

If neither is a superclass of the other, use a common superclass.
Remember that all classes are implicit subclasses of Object.
 
I

impaler

Use a factory method that returns a generic object and you will have to
do casts anyway when using that newly created object. But you have to
subclass both RimsAcyclic and Message classes.

ex:
public class RimsAcyclic extends MyGenericClass ....
public class Message extends MyGenericClass ....

public class MyFactory() {
public MyGenericClass getInstance(String asset) {
if (asset.equals("Rims_A") return new RimsAcyclic()
else return new Message();
}
}

by the way, don't forget to use equals because if ASSET contains the
string "Rims_A" it won't necessarily be '==' with "Rims_A". It checks
the references, because String is an object not a basic data type.
Watch for the most common mistakes of java beginners.

http://www.javacoffeebreak.com/articles/toptenerrors.html

more on this google for java factory pattern.
for ex: http://today.java.net/pub/a/today/2005/03/09/factory.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top