Results 1 to 3 of 3

Thread: Instanciate an object with name inside a variable

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    57

    Instanciate an object with name inside a variable

    Hello forumers !

    My problem is the following.
    I have a main class, Test, and another class called "Vehicle".

    So, the normal instanciation to create the object is:

    Vehicle car1 = new Vehicle(1, "Car1", 20); //for example...

    What I wanna do is to read from keyboard a name, put it in a variable, and then, use that variable to replace car1. For example:


    main{

    String nameInstance = "";
    nameInstance = "Ferrari";

    Vehicle nameInstance = new Vehicle(...);

    So, put the name of the variable, not the Ferrari in the isntanciation, but gives an error... Any way to do ??

    Thanks !

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Instanciate an object with name inside a variable

    Why do you want to do this?

    Typically these problems are from a design flaw and can usually be worked around in a much cleaner, and easier to maintain, way.

    If you can explain the situation in a little more detail, we might be able to help better.

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    57

    Re: Instanciate an object with name inside a variable

    Thanks for your answer

    I decided to do it in other way, thanks anyway !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width