Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Create an istance into if control.

Thread view: 
palmis - 16 Jan 2006 11:02 GMT
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.
Robert Klemme - 16 Jan 2006 11:12 GMT
> I have a problem:
>
[quoted text clipped - 16 lines]
> 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
zero - 16 Jan 2006 11:22 GMT
"Robert Klemme" <bob.news@gmx.net> wrote in news:431ddrF1l3qk5U1
@individual.net:

>> I have a problem:
>>
[quoted text clipped - 25 lines]
>
>     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.

Signature

Beware the False Authority Syndrome

impaler - 16 Jan 2006 11:46 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.