{"id":979,"date":"2010-09-06T23:53:17","date_gmt":"2010-09-06T18:23:17","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=979"},"modified":"2010-09-06T23:53:17","modified_gmt":"2010-09-06T18:23:17","slug":"invoke-method-using-reflection-in-java","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/invoke-method-using-reflection-in-java\/","title":{"rendered":"Invoke method using reflection in JAVA"},"content":{"rendered":"<p>Java reflection, is the very useful concept used by maximum professional to get the run time flexibility of calling or getting the necessary information about particular java class. It may be inbuilt JAVA class or user defined class. Reflections are mostly used in eclipse or any Java editor to provide intellisens like features.<\/p>\n<p>In below example, we have created a method named <strong>add(int a, int b) <\/strong>which is called by reflection property.<\/p>\n<p>to invoke method using java reflection <strong>method.invoke(classObj, arglist) <\/strong>is used.<\/p>\n<p><!--more--><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.shivasoft.evening.reflection;\n\nimport java.lang.reflect.Method;\n\npublic class InvokeDemo {\n\n\tpublic int add(int a, int b)\n\t{\n\t\treturn a+b;\n\t}\n\tpublic static void main(String&#x5B;] args) throws Exception {\n\t\tClass c = Class.forName(&quot;com.shivasoft.evening.reflection.InvokeDemo&quot;);\n\n\t\tClass&#x5B;] parTypes = new Class&#x5B;2];\n\t\tparTypes&#x5B;0] = Integer.TYPE;\n\t\tparTypes&#x5B;1] = Integer.TYPE;\n\n\t\t\/\/Get reference of add method\n\t\tMethod m = c.getMethod(&quot;add&quot;, parTypes);\n\n\t\tObject&#x5B;] argList = new Object&#x5B;2];\n\t\targList&#x5B;0] = 40;\n\t\targList&#x5B;1] = 90;\n\n\t\tInvokeDemo obj = new InvokeDemo();\n\t\tSystem.out.println(m.invoke(obj,argList));\n\t}\n}\n<\/pre>\n<p>Output of the above program is: <strong>130<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Invoke method using reflection in JAVA <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[329,166,173],"class_list":["post-979","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-reflection","tag-rtti"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":999,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/bouncing-ball-applet-create-balls-on-mouse-click-of-random-color-and-random-size\/","url_meta":{"origin":979,"position":0},"title":"Bouncing ball applet &#8211; Create Balls on Mouse click of random color and random size","author":"Jitendra","date":"September 9, 2010","format":false,"excerpt":"Creating the balls on mouse click of random color, random size and random speed and bouncing around the wall of applet in JAVA.","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Bouncing Ball applet - Create ball on Mouse click of random size and random color","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/09\/Bouncing-Ball-applet.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":5434,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/creating-custom-ant-plugin-or-task-in-java\/","url_meta":{"origin":979,"position":1},"title":"Creating custom ANT plugin or Task in Java","author":"Jitendra","date":"March 19, 2016","format":false,"excerpt":"Tutorial to create custom Task in ANT with complete source code","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Create custom ANT Task in Java","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/03\/Creating-ANT-plugin.gif?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":2700,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/how-to-create-immutable-class-in-java\/","url_meta":{"origin":979,"position":2},"title":"How to create Immutable Class in Java","author":"Jitendra","date":"February 3, 2012","format":false,"excerpt":"creating the Immutable class in Java with simple source code example","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":847,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/mouse-handling-in-applets-using-this\/","url_meta":{"origin":979,"position":3},"title":"Bouncing ball animation in Applet","author":"Jitendra","date":"August 25, 2010","format":false,"excerpt":"Example to show Bouncing ball animation in Applet","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Mouse handling in applets using this pointer method, Bouncing ball","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/bOUNCINGBall.jpeg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":460,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-destructor-finalize\/","url_meta":{"origin":979,"position":4},"title":"Java Destructor &#8211; finalize()","author":"Jitendra","date":"July 1, 2010","format":false,"excerpt":"Concept of destructor in JAVA. finalize() method.","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Java Destructor finalize()","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/Trash.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1943,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/what-is-the-need-to-override-hashcode-and-equals-method\/","url_meta":{"origin":979,"position":5},"title":"What is the need to Override Hashcode() and equals() method","author":"Jitendra","date":"April 11, 2011","format":false,"excerpt":"Although there are lots of material are available on internet and API document about the necessity of the overriding the hashcode() and equals() method in Java but lots of new developers still not able to understand the necessity of hashcode() method. In this article, I will try to explain step\u2026","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"How Hashcode works in java","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/How-Hashcode-works-in-java.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/979","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=979"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/979\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}