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!

5
  • 1
    you hope u explained clearly but you are not. Commented Jul 31, 2012 at 13:04
  • So are there functions level1() to level100() implemented? Commented Jul 31, 2012 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. www2.sys-con.com/itsg/virtualcd/java/archives/0305/sagar2/… Commented Jul 31, 2012 at 13:06
  • @Shark it can be done with and without reflection. Commented Jul 31, 2012 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. Commented Jul 31, 2012 at 13:11

4 Answers 4

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)

Sign up to request clarification or add additional context in comments.

Comments

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();

4 Comments

your answer seems interesting, can you please give more details on how to use it properly? maybe you have a tutorial about it? Thanks!
The answers here pretty much lay it out: stackoverflow.com/questions/2745796/…
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
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<String, Foo>, but then you're stuck with numeric indices.
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.

2 Comments

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: 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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.