How can I invoke a method when the method name is in the form of a given string

0 votes

Without knowing the class of obj, how can I call the method identified by methodName on it?​

Object obj;
String mName = "gName";

Apr 27, 2018 in Java by Parth
• 4,640 points
3,041 views

2 answers to this question.

0 votes

I tried doing it this way -

java.lang.reflect.Method methods;
try {
  methods = obj.getClass().getMethod(methodName, parameter1.class, parameter2.class, ..);
} catch (SecurityException e) { ... }
  catch (NoSuchMethodException e) { ... }

The parameters identify the very specific method you need.

Then you invoke that method by calling:

try {
  method.invoke(obj, arg1, arg2,...);
} catch (IllegalArgumentException e) { ... }
  catch (IllegalAccessException e) { ... }
  catch (InvocationTargetException e) { ... }
answered Apr 27, 2018 by developer_1
• 3,350 points
0 votes

You could probably use method invocation from reflection:

Class<?> 
c = Class.forName("class name");
Method method = c.getDeclaredMethod("method name", parameterTypes);
method.invoke(objectToInvokeOn, params);

Where:

· class name - name of the class

· objectToInvokeOn - is of type Object and is the object you want to invoke the method on

· method name -  name of the method you want to call

· parameterTypes  - is of type Class[] and declares the parameters the method takes

· params - is of type Object[] and declares the parameters to be passed to the method.

answered Aug 19, 2019 by Sirajul
• 59,230 points

Related Questions In Java

0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,140 points
4,074 views
0 votes
2 answers

How can I get the filenames of all files in a folder which may or may not contain duplicates

List<String> results = new ArrayList<String>(); File[] files = ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,920 points
1,888 views
0 votes
1 answer

How do I count the number of occurrences of a char in a String?

My 'idiomatic one-liner' for this is: int count ...READ MORE

answered Dec 30, 2020 in Java by Gitika
• 65,890 points
2,011 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
2,270 views
0 votes
1 answer

How can I make the return type of a method generic?

First of all, define callFriend: public <T extends ...READ MORE

answered May 19, 2018 in Java by sharth
• 3,370 points
776 views
0 votes
1 answer

How to calculate method execution time in Java ?

Use the following code : new Timer(""){{ ...READ MORE

answered May 25, 2018 in Java by Rishabh
• 3,620 points
1,674 views
0 votes
1 answer

How to download and save a file from Internet using Java?

public void saveUrl(final String filename, final String ...READ MORE

answered May 25, 2018 in Java by Rishabh
• 3,620 points
899 views
0 votes
1 answer

How does contains() method work in evaluating objects?

Generally you should also override hashCode() each time you ...READ MORE

answered May 25, 2018 in Java by Rishabh
• 3,620 points
991 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,140 points
3,659 views
0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,920 points
13,075 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP