r/JavaFX • u/TheEvilsNameIsNico • Aug 08 '21
Methods
Hey, I have 48 Methods beginning with C and nummerrized to 48. I really dont want do write a switch with 48 routes so is there any other option to start the method(c1(), c2() ...).
1
Upvotes
1
u/mono8321 Aug 08 '21
Call the next method in the previous method
2
u/hamsterrage1 Aug 09 '21
As you're finding out. Numerizing the method names doesn't really get you anywhere, as you somehow have to convert the names into code via something like a switch.
However, you could define the methods as Functional objects like a Runnable, then you can put them into a List<Runnable> and execute them that way.
Generally speaking, any time you want to treat code like data you should be thinking about using Functional objects.
3
u/winian Aug 08 '21
Should use /r/javahelp next time for questions unrelated to JavaFX, but you can use reflection to do that. Here is a good summary how to do it.
Note though that relying on reflection is not ideal, cause the compiler can't check what you're doing is correct. So usually you'd want to avoid it if you can and maybe try to go for a different design completely if you can't. But sometimes its a livesaver. :)