{"id":663,"date":"2017-05-05T08:40:17","date_gmt":"2017-05-05T08:40:17","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=663"},"modified":"2025-06-02T07:34:19","modified_gmt":"2025-06-02T07:34:19","slug":"what-is-java-reflection","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/what-is-java-reflection\/","title":{"rendered":"What is Java Reflection?"},"content":{"rendered":"<h1>What is Java Reflection?<\/h1>\n<p><strong data-start=\"0\" data-end=\"19\">Java Reflection<\/strong> is a powerful feature in the Java programming language that allows a program to inspect and manipulate the <strong data-start=\"127\" data-end=\"147\">runtime behavior<\/strong> of classes, methods, fields, and constructors \u2014 even if they are <strong data-start=\"213\" data-end=\"224\">private<\/strong>.<\/p>\n<p><strong>Java Reflection<\/strong> is a mechanism for manipulating classes, interfaces, and objects at the runtime of the Java program. Reflection API consists of methods to introspect the class at runtime.<\/p>\n<h2 data-start=\"227\" data-end=\"266\">Features of Java Reflection<\/h2>\n<p>Using Reflection you can create an instance of a class whose name is not known until runtime. Invoke methods on an object, set and get accessors and mutators for an object&#8217;s field which is unknown until runtime.<\/p>\n<ul>\n<li data-start=\"270\" data-end=\"334\"><strong data-start=\"270\" data-end=\"322\">Inspect classes, interfaces, fields, and methods<\/strong> at runtime.<\/li>\n<li data-start=\"338\" data-end=\"412\"><strong data-start=\"338\" data-end=\"356\">Create objects<\/strong>, <strong data-start=\"358\" data-end=\"376\">invoke methods<\/strong>, and <strong data-start=\"382\" data-end=\"399\">access fields<\/strong> dynamically.<\/li>\n<li data-start=\"416\" data-end=\"472\">Allows working with <strong data-start=\"436\" data-end=\"471\">classes unknown at compile time<\/strong>.<\/li>\n<li data-start=\"476\" data-end=\"558\">Can <strong data-start=\"480\" data-end=\"512\">bypass access control checks<\/strong>, such as accessing private fields or methods.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1411\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-Reflection.png\" alt=\"Java Reflection\" width=\"846\" height=\"563\" title=\"\"><\/p>\n<h2 data-start=\"565\" data-end=\"615\"><code class=\"\" data-line=\"\">java.lang.reflect<\/code> Package<\/h2>\n<p>To import and use Reflection API we have to use the below statement.<\/p>\n<p><strong>import java.lang.reflect.*;<\/strong><\/p>\n<ul>\n<li data-start=\"618\" data-end=\"625\"><code class=\"\" data-line=\"\">Class<\/code><\/li>\n<li data-start=\"628\" data-end=\"636\"><code class=\"\" data-line=\"\">Method<\/code><\/li>\n<li data-start=\"639\" data-end=\"646\"><code class=\"\" data-line=\"\">Field<\/code><\/li>\n<li data-start=\"649\" data-end=\"662\"><code class=\"\" data-line=\"\">Constructor<\/code><\/li>\n<li data-start=\"665\" data-end=\"675\"><code class=\"\" data-line=\"\">Modifier<\/code><\/li>\n<\/ul>\n<h2><strong>Class object<\/strong><\/h2>\n<p>For every loaded class, the Java Runtime Environment maintains an associated Class object. The Class object \u201creflects\u201d the class it represents You can use the Class object to discover information about a loaded class<br \/>\n\u2022 name<br \/>\n\u2022 modifiers<br \/>\n\u2022 superclasses<br \/>\n\u2022 implemented interfaces<br \/>\n\u2022 fields<br \/>\n\u2022 methods<br \/>\n\u2022 constructors<\/p>\n<p>Sample ways to access the Class object for a loaded class<\/p>\n<p>To get the Class object for an object <strong>notKnownAtRuntime<\/strong><\/p>\n<p><strong>Class clz = notKnownAtRuntime.getClass();<\/strong><\/p>\n<h2>Dynamic Class loading<\/h2>\n<p>The <strong>Class.forName(&#8220;classname&#8221;)<\/strong> method in Java is used to dynamically load a class at runtime.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p><strong>Class.forName(&#8220;fully.qualified.ClassName&#8221;);<\/strong><\/p>\n<p>It loads the class specified by the string name.<\/p>\n<p>It initializes the class (runs the static block and static initializations).<\/p>\n<p>It returns the Class object associated with the class.<\/p>\n<p>Example:<\/p>\n<p><strong>Class clz = Class.forName(\u201ccom.testingdocs.sample.program.SampleClass\u201d);<\/strong><\/p>\n<p>&nbsp;<\/p>\n<h2><strong>Java Program<\/strong><\/h2>\n<p>In the below program we will print all the method names of Object class.<\/p>\n<pre><code class=\"\" data-line=\"\">import java.lang.reflect.*; \n\npublic class ReflectionExample { \n\npublic static void main(String[] args) { \ntry { \n    Object obj = new Object(); \n     printMethodNames(obj); \n    } \ncatch (Exception e) { \n    System.out.println(e); \n   } \n}<\/code><\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\"><\/pre>\n<pre><code class=\"\" data-line=\"\">static void printMethodNames(Object o) { \n\nClass&lt;?&gt; c = o.getClass(); \nSystem.out.println(&quot;Method names of :&quot; + c.getName() + &quot; class&quot;); \n\nMethod[] methods = c.getMethods(); \nfor (int i = 0; i &lt; methods.length; i++) { \n    String methodName = methods[i].getName();                                \n    System.out.println(methodName + &quot;()&quot;);\n    }\n } \n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2><strong>Program output<\/strong><\/h2>\n<p>Method names of :java.lang.Object class<br \/>\nwait()<br \/>\nwait()<br \/>\nwait()<br \/>\nequals()<br \/>\ntoString()<br \/>\nhashCode()<br \/>\ngetClass()<br \/>\nnotify()<br \/>\nnotifyAll()<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1415\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Java-Reflection-Program.png\" alt=\"Java Reflection Program\" width=\"1297\" height=\"649\" title=\"\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Java Reflection? Java Reflection is a powerful feature in the Java programming language that allows a program to inspect and manipulate the runtime behavior of classes, methods, fields, and constructors \u2014 even if they are private. Java Reflection is a mechanism for manipulating classes, interfaces, and objects at the runtime of the Java [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-663","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=663"}],"version-history":[{"count":17,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/663\/revisions"}],"predecessor-version":[{"id":27552,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/663\/revisions\/27552"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}