Results 1 to 10 of 10
Thread: Dynamically calling method
- 10-23-2010, 05:39 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
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...
- 10-23-2010, 06:00 PM #2
- 10-23-2010, 06:06 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
I don't see how that would solve the problem. Example?
-
- 10-23-2010, 07:07 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
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.
... is the nearest I come, but it is pseudocode.Java Code:Car[B](choice)[/B] volvo/bmw = new Car[B](choice)[/B]()
... is what the code should execute.Java Code:Car2 bmw = new Car2()
Last edited by stian.hoiland; 10-23-2010 at 07:17 PM.
-
The name of the variable means nothing. And since all your Car classes derive from the parent class Car (I assume), you could do:
Another possible solution is to create and use a factory pattern.Java Code:Car car = null; switch (choice) { case 1: car = new Car1(); break; case 2: car = new Car2(); break; }
- 10-23-2010, 07:19 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
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
-
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
- 10-23-2010, 07:38 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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
-
Similar Threads
-
Thread problem, calling method in run method
By majk in forum Threads and SynchronizationReplies: 4Last Post: 09-27-2010, 11:40 AM -
Calling The main method from another method
By SwissR in forum New To JavaReplies: 3Last Post: 07-27-2010, 11:03 AM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
Object from String (calling method dynamically)
By Java Tip in forum Java TipReplies: 0Last Post: 02-16-2008, 09:22 PM -
Dynamically calling a method with 2 argumens
By Java Tip in forum Java TipReplies: 0Last Post: 02-15-2008, 08:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks