Results 1 to 4 of 4
- 02-23-2011, 05:48 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 61
- Rep Power
- 0
how to enter input method name for getMethod(------)
package getClass;
import java.lang.reflect.Method;
import java.util.Scanner;
class H
{
void test1()}
{
System.out.println("test1()");
}
void test2()
{
System.out.println("test2()");
}
class Manager10
{
public static void main(String[] args)throws Exception}
{
Scanner sc = new Scanner(System.in);
System.out.println("eneter class name");
String className = sc.next();
Class c1 = Class.forName(className);
H h1 = (H)c1.newInstance();
System.out.println("enter method name");
String methodName = sc.next();
Method m1 = c1.getMethod(methodName);
m1.invoke(h1);
}
OUTPUT
eneter class name
getClass.H
enter method name
test1()
Exception in thread "main" java.lang.NoSuchMethodException: getClass.H.test1()()
at java.lang.Class.getMethod(Unknown Source)
at getClass.Manager10.main(Manager10.java:31)
when i run the program the above output has resulted,but test1() which is in class H is not identified when i gave the input test1() for the above prog....can any one help me out how to pass the method name.???
how to pass the method name(test1() or test2()) as input for getMethod()Last edited by Dayanand; 02-23-2011 at 06:00 PM.
- 02-23-2011, 05:52 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
first, the input for the methodname should be only test1, without the brackets !!
second, the getMethod method is only for public members(read the API doc). You should use c1.getDeclaredMethod(methodName); !
- 02-23-2011, 06:03 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 61
- Rep Power
- 0
- 02-23-2011, 06:07 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Similar Threads
-
Http Input Stream read method
By chandan.mishra in forum Advanced JavaReplies: 2Last Post: 01-03-2011, 02:36 AM -
input method help?
By blazinhieu in forum New To JavaReplies: 7Last Post: 03-12-2010, 03:07 PM -
More than one input in one private method
By rice in forum New To JavaReplies: 0Last Post: 10-02-2009, 05:08 AM -
help in setMethod and getMethod
By lclclc in forum New To JavaReplies: 5Last Post: 09-28-2009, 02:34 PM -
Input parameter of Main method
By Java Tip in forum Java TipReplies: 1Last Post: 07-12-2008, 06:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks