Suppose you have 3 types of resources (String, Drawable and Raw) for 50 kinds of animals like the following:
And suppose you want just 3 resources (String, Drawable and Raw) to be loaded for the ONE specific animal which the user will choose ONE at a time - that's gonna be like 50 if-else conditional statements with 3 resource definitions each = 150 lines of code.
With the following example, you can reduce it down to just about 36 lines of code to do exactly that.
Showing posts with label passing. Show all posts
Showing posts with label passing. Show all posts
Thursday, August 25, 2011
Wednesday, August 24, 2011
Calling Java methods dynamically by passing a String of the method name
Imagine you have 99 distinct methods:
- method1()
- method2()
- method3()
- method4()
- ...
- ...
- ...
- method99()
and each of these methods are distinct methods with distinct functionality which will be executed for the 99 distinct return values of your getID() method.
So typically you would have to write 99 if-else condition statements. What if you could simply do the same thing in one line instead of defining 99 if-else condition statements.
call_method ("method" + getID());
The following code snippet is a simple tutorial of how to use STRING parameters for calling methods dynamically in java.
Subscribe to:
Posts (Atom)