• Post Reply Bookmark Topic Watch Topic
  • New Topic

i have problem with getMethod() and getDeclaredMethod()  RSS feed

 
Nada Mohammed
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i need to invoke method from class but before that i need to get the method my problem is
when i write :
System.out.println("method name " + T.getClass().getDeclaredMethod("MethodName",null));
null because method MethodName it takes no arguments.
the result is :
method name boolean className.MethodName()
boolean because the method return boolean type.
BUT when i write :
System.out.println("method name " + T.getClass().getMethod("MethodName",null));
the result in the run time is :
Exception : java.lang.NoSuchMethodException: MethodName
<<< i hope i get any body explain for me why this happen and give me solution >>>
<<< Thanks a lot >>>
 
Jerry Pulley
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best guess is that the method isn't . only works for methods (although it covers inherited methods); works for any method declared in the class (but not for inherited ones).
jply
P.S. Moderators - see how silly this looks with the UBB-style code tags? HTML is better. I understand the desire for convenient inline HTML, but this being a Java forum it seems that inline Java code is more useful.

[This message has been edited by Jerry Pulley (edited October 02, 2000).]
 
Klaudia Mat
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried
System.out.println("method name " + T.getDeclaredMethod("MethodName",null));
? it works for me if T is an Object.

Also if you meant T is the name of a class (no matter if "MethodName" is static or not) it should work as this:
System.out.println("method name " + T.class.getDeclaredMethod("MethodName",null));

As i guess, your way looks for a method called "MethodName" in class "Class".

Nada Mohammed wrote:Hi,
i need to invoke method from class but before that i need to get the method my problem is
when i write :
System.out.println("method name " + T.getClass().getDeclaredMethod("MethodName",null));
null because method MethodName it takes no arguments.
the result is :
method name boolean className.MethodName()
boolean because the method return boolean type.
BUT when i write :
System.out.println("method name " + T.getClass().getMethod("MethodName",null));
the result in the run time is :
Exception : java.lang.NoSuchMethodException: MethodName
<<< i hope i get any body explain for me why this happen and give me solution >>>
<<< Thanks a lot >>>
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!