0

I would like to save some work on my app, is it possible to get the string, for example "level1" and then use the corresponding function, which would be level1();? my main point is not to make a huge switch-case statement, but only make a few level functions in a storage class, and whenever you level up, the string would change to "level" + number where number is the int, so lets say that right now you are in level 10, the function that would run is level10();

I hope i explained it clearly.. sorry if not.. hope you get the idea!

Thanks!

Baruch
  • 1,618
  • 5
  • 23
  • 42
  • 1
    you hope u explained clearly but you are not. – Nandkumar Tekale Jul 31 '12 at 13:04
  • So are there functions level1() to level100() implemented? – yhyrcanus Jul 31 '12 at 13:05
  • I don't think you'll be able to do it without using reflection. Heck, i'm not sure if you'll be able to do it WITH reflection either. http://www2.sys-con.com/itsg/virtualcd/java/archives/0305/sagar2/index.html – Shark Jul 31 '12 at 13:06
  • @Shark it can be done with and without reflection. – Matt Ball Jul 31 '12 at 13:08
  • I'm more of a "wouldn't venture into reflection unless necessary" mindset, so I'd find a "better" way to design and organize my code... E.g. your answer below. – Shark Jul 31 '12 at 13:11

4 Answers4

3

I believe you want to call a method at runtime using its name as a string.

You can do it via reflection.

Class.getMethod(String methodName, Class... parameterTypes)

Benito Bertoli
  • 25,285
  • 12
  • 54
  • 61
2

Don't think of this in terms of method names, unless you want to muck around with reflection (you don't want to, and it's not necessary).

If you really do need to convert strings to method calls – and that's a big "if" – create a Map<String, Foo> where Foo implements some "callable"-like interface. Then a string-to-method lookup is simply:

Map<String, Foo> commands = /* ... */;
Foo foo = commands.get("level42");
foo.bar();
Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • your answer seems interesting, can you please give more details on how to use it properly? maybe you have a tutorial about it? Thanks! – Baruch Jul 31 '12 at 13:21
  • The answers here pretty much lay it out: http://stackoverflow.com/questions/2745796/how-can-i-map-a-string-to-a-function-in-java – Matt Ball Jul 31 '12 at 13:27
  • I am afraid that it would slow down my app since this is supposed to call itself over and over again countless times in my game loop, i need it to redraw the level every time, i guess i'll move back to switch case and a function. Thanks anyways – Baruch Jul 31 '12 at 13:41
  • A map provides constant time lookup. If that's too slow (e.g. the constant is too large) you can use a `Foo[]` (an array) instead of a `Map`, but then you're stuck with numeric indices. – Matt Ball Jul 31 '12 at 13:44
1

It really sounds like you should just have a

void setLevel(int level)

call. That can feel free to ignore (say) levels 11-14 or whatever... but it would be very ugly to have separate methods and invoke them by name. You can do so with reflection, but you should think about other options first.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I know that it might look a bit ugly, but the problem with your suggestion is that i would probably end up with a huge Switch-Case statement/huge "IF-ELSE" statement which i am trying to avoid.. Thanks anyway – Baruch Jul 31 '12 at 13:12
  • @Baruch: I believe that would still be cleaner than calling methods by reflection. You can always break out significant chunks of code into separate methods *called* by `setLevel`. – Jon Skeet Jul 31 '12 at 13:21
0

Please see the top answer to this post:

Java dynamic function calling

I would also recommend following their advice regarding structure, to create a more object-oriented solution instead of using reflection.

Community
  • 1
  • 1
LJ2
  • 593
  • 5
  • 11