Results 1 to 10 of 10
  1. #1
    stian.hoiland is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Question Dynamically calling method

    Java Code:
    int choice = 0;
    Scanner scanner = new Scanner(System.in);
    choice = scanner.nextInt();
    (...)
    Method[B](choice)[/B] method = new Method[B](choice)[/B]();

    The problem here is the "(choice)" part. How can you do this whitout a switch?

    In the case of choice == 1:
    Method(choice) should be "Method1".
    In the case of choice == 2:
    Method(choice) should be "Method2".
    etc...

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default

    Quote Originally Posted by stian.hoiland View Post
    How can you do this whitout a switch?
    if-else?

    db

  3. #3
    stian.hoiland is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    I don't see how that would solve the problem. Example?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by stian.hoiland View Post
    I don't see how that would solve the problem. Example?
    ?
    Do you understand if/else blocks? A switch statement is not much more than a glorified bunch of if/else blocks, also known as syntactic sugar. I think you should be able to create an example yourself... why not give it a try?

  5. #5
    stian.hoiland is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    As far as I can see the problem is not what kind of loop to use.

    Java Code:
    int choice = 0;
    Scanner scanner = new Scanner(System.in);
    choice = scanner.nextInt(); //capture choice
    (...)
    Car[B](choice)[/B] volvo = new Car[B](choice)[/B](); //contruct Car based on choice. NOTE: this is pseudocode.

    There will be different Cars in different files called Car1, Car2, etc... I want to create the right Car, and that is the Car choosen by the user and stored in choice.


    The obvious way to do this is:

    Java Code:
    switch (choice) {
                case 1:  Car[B][U]1[/U][/B] volvo = new Car[B][U]1[/U][/B](); break;
                case 2:  Car[B][U]2[/U][/B] bmw = new Car[B][U]2[/U][/B](); break;
    }

    ... but this isn't dynamic enough for me. You would have to hardcode the number of Cars, but this code is meant for a framework with an unknown/dynamic number of files containing Cars.


    The above example might not make much sense semantically, but it is only an example.


    Recap:

    Files:
    Car1.java - contains properties of Car1
    Car2.java - contains properties of Car2

    Program:
    1. list avalible cars (Car1 and Car2)
    2. capture choice of car (e.g. 2)
    3. get properties from the choosen car (construct Car2)

    Step 3 is where I'm stuck because I cannot find any code to do this.
    Java Code:
    Car[B](choice)[/B] volvo/bmw = new Car[B](choice)[/B]()
    ... is the nearest I come, but it is pseudocode.
    Java Code:
    Car2 bmw = new Car2()
    ... is what the code should execute.
    Last edited by stian.hoiland; 10-23-2010 at 07:17 PM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The name of the variable means nothing. And since all your Car classes derive from the parent class Car (I assume), you could do:

    Java Code:
    Car car = null;
    switch (choice) {
      case 1: 
        car = new Car1();
        break;
      case 2:
        car = new Car2();
        break;
    }
    Another possible solution is to create and use a factory pattern.

  7. #7
    stian.hoiland is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    You are using a switch and therefore have to hardcode the number of Cars, which will not be known. What is a factory pattern? :o

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    A factory pattern is a way of encapsulating the creation of objects, and information on it can be found here: Factory pattern - Wikipedia, the free encyclopedia

    Are you dynamically loading classes via a class loader? If so then perhaps you need to do this with reflection as a way solve your problem. For e.g.,

    Dynamic Class Loading using Java Reflection API. Java Reflection API Tutorial | ViralPatel.net
    Java Reflection: Dynamic Class Loading and Reloading

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    ?
    Do you understand if/else blocks? A switch statement is not much more than a glorified bunch of if/else blocks, also known as syntactic sugar.
    That is not enirely true; there's the table based switch and the 'direct' switch statement. The first form used a lookup table (ints are the keys, the addresses of the corresponding case statements form the associated values). The 'direct' form uses the int in the switch clause as an index in an array of target addresses. A bunch of if-else-if ... tests simply sequentially tests all conditions. imho this is not just syntactic sugar.

    kind regards,

    Jos

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 11:40 AM
  2. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 11:03 AM
  3. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  4. Object from String (calling method dynamically)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 09:22 PM
  5. Dynamically calling a method with 2 argumens
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 08:49 AM

Posting Permissions

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