Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: how to call an object with a string or variable?

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default how to call an object with a string or variable?

    Hi all,

    i'd like to call an object with a String but I haven't found anything about it, how to do that.

    So, you have a process, and its result is, that you get a String (result) filled with the characters lets say "house"

    You have an Object named 'house' too.

    Now, I would like to call the 'house' object, because String (result) = house after.

    If the process gives the result 'car', than the program should call not the house object but the car object.

    So for example:

    String result

    car called Object;

    process gives a result (it can be car, or house, or whatever, ((the result is random choice from a defined number of result possibilities)) )

    String result = car

    calledObject = new car(); // here is the point, i don't now if the result is car or house, so for me it would look like this:
    // callesObject = new result(); - so that the call calles the objcetname which the process gives


    So:

    So after a process I get a value for a String. And than I would like to call the Object whichs name is the same like the value of the String. How do I do that?

    I hope you understand my issue, im total beginner, so sorry for the poor explanation!

    Thank You very much in advance for replying!

    Zoli


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: how to call an object with a string or variable?

    I'm not sure I understand, but why can't you just use a switch or an 'if' statement to create the object based on 'result'?
    if ( result.equals( "car" )
    {
        new Car();
    }
    Can you create a short code-like or pseudo code example to demonstrate what you mean?

    --- Update ---

    Sorry, missed a close paren:
    if ( result.equals( "car" ) )
    {
        new Car();
    }

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Zoli (September 14th, 2013)

  4. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: how to call an object with a string or variable?

    Hi Greg, thank you for your answer!!

    Yes it would be a good solution for the first version, but later than I have to write a program, which re-writes the main program if the group of possible result will grow, and in the future it should be a very big group, like over 4000 members (later even more). So the best would be to be able to open the class with a call, in which I can use the string variable instead of giving the exact name of the object, as I don't know what the result will be.

    like if:

    String result;

    some process that gives a value to result (car, house, dog, garden ... ...)

    new result ();

    would call the object that has the same name like the in result stored word. So new result (); could be: new house (); or: new house(); etc..

    Or is this question if i would ask if there was a command like: do what i want (); and the program would do it...

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: how to call an object with a string or variable?

    Okay, rather than an if/switch statement with 4,000+ possible results, I recommend some kind of an event-driven system that responds appropriately to the 'result' events as they occur. An EventBus comes to mind, but there may be better options, because an EventBus with 4,000+ events is a bit ridiculous.

    I'm having trouble imagining the situation where you'd have 4,000+ unique results to respond to which means 4,000+ unique objects to create to correspond to each of the 4,000+ results, etc. I'll accept it if you say it's so, but I suspect the problem can be simplified significantly.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    Zoli (September 14th, 2013)

  7. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: how to call an object with a string or variable?

    You can do something like this:
    Car someCar = (Car) create("Car");
     
    public Object create(String className) {
        return Class.forName(className).newInstance();
    }
    But thats usually not a "safe" way to code.

  8. The Following User Says Thank You to Cornix For This Useful Post:

    Zoli (September 14th, 2013)

  9. #6
    Junior Member
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: how to call an object with a string or variable?

    Thank you guys for the answers, i will experiment a little bit with it!

    Thanks for the ideas!

    --- Update ---

    Or is it possible to write a code that modifies the code itself?
    (and anyways I came to the thing, that a create simple files instead of objects, so the problem is now: how to open a file with a string?)

    So i imagine it like this:

    i have txt files like car.txt, house.txt etc...

    String result;

    prozess gives a value to result

    and here it would be great to have a command which would implement the text i give in into the program itself to the next line like:

    write.programitslef("Scanner " + result + "Scanner = new Scanner (new File ("" + result + ".txt"));")

    So if you run it, after you have for example 'car' for the String, to code writes to itself:

    Scanner carScanner = new Scanner (new File ("car.txt));

    so i could reach the stored information in the actual file, without knowing it in advance which i'll need

    --- Update ---

    i mean:

    Scanner carScanner = new Scanner (new File ("car.txt"));

Similar Threads

  1. static variable accessible in object b, not in object a?
    By Pajaro in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 30th, 2013, 02:41 PM
  2. how to call a string inside a while loop
    By shen_punkz21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2013, 07:25 AM
  3. Call a method in all instances of an object
    By MaximusPrime in forum Java Theory & Questions
    Replies: 2
    Last Post: March 14th, 2012, 10:39 PM
  4. Is this a method call or variable declaration ???
    By hoboy in forum Java Theory & Questions
    Replies: 8
    Last Post: March 11th, 2012, 10:32 AM
  5. Saving string value of an iterator Object into a variable.
    By apocalipsis1982 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: December 28th, 2011, 01:12 PM