Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List functions as selections in Java

Tags:

java

I am a Java newbie so my questions may look like an easy one. But I need some direction from your guys.

Here is my question: I have a class with bunch of methods, I would like to give these methods to user in a combox to select, based on their selection some code will run. Now I can do this by writing switch selection method. Where based on selection I use switch to run a particular method.

But my list of functions is pretty long close to 200, SO my questions to you is: is there a smarter way of doing this. Just point me to the right direction and I'll try to do the rest.

like image 289
user1453817 Avatar asked Jun 13 '12 13:06

user1453817


People also ask

What are the different types of selection statements in Java?

There are three selection statements in Java: if, if..else, and switch. Let's take a closer look at them. 1. The if Statement This is a single selection statement. It is named so because it only selects or ignores a single action (or group of actions).

What is the use of list in Java?

Java List. List in Java provides the facility to maintain the ordered collection. It contains the index-based methods to insert, update, delete and search the elements. It can have the duplicate elements also. We can also store the null elements in the list. The List interface is found in the java.util package

How to pick an item from a list in Java?

The basic idea for pick an item from list is, First generate a number which should be between 0 to list size. First we select a random index for using Random.nextInt(int bound) method. Instead of Random class, you can always use static method Math.random()(random() method generate an number between 0 to 1) and multiply it with list size.

What are the characteristics of list in Java?

Some of the characteristics of the list in Java include: Lists can have duplicate elements. The list can also have ‘null’ elements. Lists support generics i.e. you can have generic lists. You can also have mixed objects (objects of different classes) in the same list.


2 Answers

You can use reflection, specifically: Class.getMethods() or Class.getDeclaredMethods().

Make sure you understand the differences between them (read the linked javadocs for this), if you don't - don't be afraid to ask.

like image 130
amit Avatar answered Nov 15 '22 00:11

amit


I think looking into Java Reflection would be the best place to start, assuming I've understood what you want to do correctly.

like image 29
Anthony Grist Avatar answered Nov 14 '22 23:11

Anthony Grist