{"id":1653,"date":"2022-08-30T15:18:19","date_gmt":"2022-08-30T15:18:19","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/java-error-the-method-getmethodstring-classboolean-is-undefined-for-type-class-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:18:19","modified_gmt":"2022-08-30T15:18:19","slug":"java-error-the-method-getmethodstring-classboolean-is-undefined-for-type-class-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/java-error-the-method-getmethodstring-classboolean-is-undefined-for-type-class-collection-of-common-programming-errors\/","title":{"rendered":"Java Error: The method getMethod(String, Class&lt;boolean&gt;) is undefined for type Class-Collection of common programming errors"},"content":{"rendered":"<p>The compile-time error suggests, that you are using Java 1.4 to compile the class. Now, in Java 1.4, it was illegal to define array parameters as <code>Type...<\/code>, you had to define them as <code>Type[]<\/code>, and this is the way the <code>getMethod<\/code> is defined for the <code>Class<\/code>:<\/p>\n<pre><code>Method getMethod(String name, Class[] parameterTypes)\n<\/code><\/pre>\n<p>Because of that, you cannot use the simplified 1.5 syntax and write:<\/p>\n<pre><code>MyClass.class.getMethod(\"myMethod\",boolean.class));\n<\/code><\/pre>\n<p>what you need to do is:<\/p>\n<pre><code>MyClass.class.getMethod(\"myMethod\",new Class[] {boolean.class}));\n<\/code><\/pre>\n<h2>Update 1<\/h2>\n<p>The code you posted, does not compile because of another reason:<\/p>\n<pre><code>super(new ClickHandler() {\n\n    \/\/ This is anonymous class body \n    \/\/ You cannot place code directly here. Embed it in anonymous block, \n    \/\/ or a method.\n\n    try {\n        OtherClass.responseMethod(\n            MyClass.class.getMethod(\"myMethod\",boolean.class));\n    } catch (Exception e) {\n        e.printStackTrace();\n    }\n});\n<\/code><\/pre>\n<p>What you should do is create a <code>ClickHander<\/code> constructor that accepts a <code>Method<\/code>, like this<\/p>\n<pre><code>public ClickHandler(Method method) {\n\n    try {\n        OtherClass.responseMethod(method);\n    } catch (Exception e) {\n        e.printStackTrace();\n    }\n\n}\n<\/code><\/pre>\n<p>and then, in <code>MyClass<\/code> constructor invoke it like this:<\/p>\n<pre><code>public MyClass() {\n    super(new ClickHandler(MyClass.class.getMethod(\"myMethod\",boolean.class)));\n}\n<\/code><\/pre>\n<h2>Original answer<\/h2>\n<p>More to this, from the JavaDoc of <code>Class#getMethod(String, Class...)<\/code><\/p>\n<blockquote>\n<p>Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.<\/p>\n<\/blockquote>\n<p>And your method is <code>private<\/code>, not <code>public<\/code>.<\/p>\n<p>If you want to get access to private methods, you should rather use <code>Class#getDeclaredMethod(String, Class...)<\/code> and make it accessible by calling <code>setAccessible(true)<\/code>.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-27 12:25:02. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>The compile-time error suggests, that you are using Java 1.4 to compile the class. Now, in Java 1.4, it was illegal to define array parameters as Type&#8230;, you had to define them as Type[], and this is the way the getMethod is defined for the Class: Method getMethod(String name, Class[] parameterTypes) Because of that, you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1653","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1653"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1653\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}