{"id":48283,"date":"2021-10-28T12:17:15","date_gmt":"2021-10-28T06:47:15","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/reflection-in-java\/"},"modified":"2025-07-03T20:02:35","modified_gmt":"2025-07-03T14:32:35","slug":"reflection-in-java","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/reflection-in-java\/","title":{"rendered":"Reflection in Java with Examples"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"what-is-reflection-in-java\">What is Reflection in Java?<\/h2>\n\n\n\n<p>Reflection is a powerful feature in Java. It provides the ability for a Java program to inspect and manipulate its own structure during execution. You can find information about classes, such as their names, methods, and fields, without knowing them at compile time.<\/p>\n\n\n\n<p>Reflection offers several key benefits:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Extensibility Features:<\/b> Frameworks like Spring and Hibernate use reflection. They read annotations and class structures to provide features like dependency injection or object-relational mapping (ORM).<\/li>\n\n\n\n<li><b>Debugging Tools:<\/b> Debuggers use reflection to inspect object states and method calls at runtime.<\/li>\n\n\n\n<li><b>Testing Frameworks:<\/b> JUnit uses reflection to find and execute test methods within a class.<\/li>\n\n\n\n<li><b>Dynamic Code Loading:<\/b> You can load classes dynamically based on user input or configuration. This allows for more flexible applications.<\/li>\n\n\n\n<li><b>Serialization:<\/b> Libraries that serialize objects to JSON or XML often use reflection to access object fields.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"is-reflection-an-api\">Is Reflection an API?<\/h2>\n\n\n\n<p>Reflection is not an external API like a web service or a third-party library. Instead, Reflection is a core feature of the Java language and its standard library. The <code>java.lang.reflect<\/code> package provides the classes and <a href=\"https:\/\/www.mygreatlearning.com\/blog\/interface-in-java\/\">interfaces<\/a> that enable this feature. These classes collectively form the Application Programming Interface (API) for using reflection within your Java programs. So, while you use an API to work with reflection, reflection itself is a built-in capability of Java.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"core-reflection-classes\">Core Reflection Classes<\/h2>\n\n\n\n<p>The <code>java.lang.reflect<\/code> package provides the classes for reflection. Here are the main ones:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Class<\/code>: This is the entry point for reflection. It represents classes and interfaces in a running Java application.<\/li>\n\n\n\n<li><code>Constructor<\/code>: This class represents a single constructor of a class.<\/li>\n\n\n\n<li><code>Method<\/code>: This class represents a single method of a class or interface.<\/li>\n\n\n\n<li><code>Field<\/code>: This class represents a single field (a member variable) of a class or interface.<\/li>\n<\/ul>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy Pro<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-java-programming\" class=\"courses-cta-title-link\">Java Programming Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Learn Java the right way! Our course teaches you essential programming skills, from coding basics to complex projects, setting you up for success in the tech industry.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>16.05 Hrs<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>3 Projects<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-java-programming\" class=\"courses-cta-button\">\n                Learn Java Programming\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-use-reflection\">How to Use Reflection<\/h2>\n\n\n\n<p>You start reflection by getting a <code>Class<\/code> object. There are three common ways to do this.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-using-class-forname\">1. Using Class.forName()<\/h3>\n\n\n\n<p>This method is useful when you have the class name as a string.<\/p>\n\n\n\n<p>Java<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic class ReflectionClassForName {\n    public static void main(String&#x5B;] args) {\n        try {\n            Class&amp;lt;?&gt; myClass = Class.forName(&quot;java.lang.String&quot;);\n            System.out.println(&quot;Class Name: &quot; + myClass.getName());\n        } catch (ClassNotFoundException e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/pre><\/div>\n\n\n<p>This code loads the <code>String<\/code> class. It then prints its full name.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-using-object-getclass\">2. Using object.getClass()<\/h3>\n\n\n\n<p>If you already have an object instance, you can get its <code>Class<\/code> object.<\/p>\n\n\n\n<p>Java<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic class ReflectionGetObjectClass {\n    public static void main(String&#x5B;] args) {\n        String myString = &quot;Hello Reflection&quot;;\n        Class&amp;lt;?&gt; myClass = myString.getClass();\n        System.out.println(&quot;Class Name: &quot; + myClass.getName());\n    }\n}\n<\/pre><\/div>\n\n\n<p>This example gets the <code>Class<\/code> object for a <code>String<\/code> instance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-using-class-literal\">3. Using .class Literal<\/h3>\n\n\n\n<p>This is the simplest way when you know the class at compile time.<\/p>\n\n\n\n<p>Java<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic class ReflectionClassLiteral {\n    public static void main(String&#x5B;] args) {\n        Class&amp;lt;?&gt; myClass = Integer.class;\n        System.out.println(&quot;Class Name: &quot; + myClass.getName());\n    }\n}\n<\/pre><\/div>\n\n\n<p>This code directly gets the <code>Class<\/code> object for <code>Integer<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"examples-of-reflection-in-action\">Examples of Reflection in Action<\/h2>\n\n\n\n<p>Let us explore practical examples using reflection.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-1-inspecting-class-fields\">Example 1: Inspecting Class Fields<\/h3>\n\n\n\n<p>You can get information about a class fields, including private ones.<\/p>\n\n\n\n<p>Java<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport java.lang.reflect.Field;\nclass Person {\n    private String name;\n    public int age;\n\n    public Person(String name, int age) {\n        this.name = name;\n        this.age = age;\n    }\n}\npublic class ReflectFields {\n    public static void main(String&#x5B;] args) {\n        try {\n            Class&amp;lt;?&gt; personClass = Class.forName(&quot;Person&quot;);\n\n            System.out.println(&quot;Public Fields:&quot;);\n            Field&#x5B;] publicFields = personClass.getFields(); \/\/ Gets public fields\n            for (Field field : publicFields) {\n                System.out.println(&quot;  &quot; + field.getName());\n            }\n\n            System.out.println(&quot;\\nDeclared Fields (including private):&quot;);\n            Field&#x5B;] allFields = personClass.getDeclaredFields(); \/\/ Gets all declared fields\n            for (Field field : allFields) {\n                System.out.println(&quot;  &quot; + field.getName());\n            }\n\n        } catch (ClassNotFoundException e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/pre><\/div>\n\n\n<p>This code retrieves and prints both public and private fields of the <code>Person<\/code> class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-2-inspecting-class-methods\">Example 2: Inspecting Class Methods<\/h3>\n\n\n\n<p>You can list all methods in a class.<\/p>\n\n\n\n<p>Java<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport java.lang.reflect.Method;\nclass Calculator {\n    public int add(int a, int b) {\n        return a + b;\n    }\n\n    private void secretMethod() {\n        System.out.println(&quot;This is a secret method.&quot;);\n    }\n}\npublic class ReflectMethods {\n    public static void main(String&#x5B;] args) {\n        try {\n            Class&amp;lt;?&gt; calculatorClass = Class.forName(&quot;Calculator&quot;);\n\n            System.out.println(&quot;Public Methods:&quot;);\n            Method&#x5B;] publicMethods = calculatorClass.getMethods(); \/\/ Gets public methods, including inherited\n            for (Method method : publicMethods) {\n                System.out.println(&quot;  &quot; + method.getName());\n            }\n\n            System.out.println(&quot;\\nDeclared Methods (including private):&quot;);\n            Method&#x5B;] declaredMethods = calculatorClass.getDeclaredMethods(); \/\/ Gets all declared methods\n            for (Method method : declaredMethods) {\n                System.out.println(&quot;  &quot; + method.getName());\n            }\n\n        } catch (ClassNotFoundException e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/pre><\/div>\n\n\n<p>This example shows how to get both public and all declared methods of a class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-3-invoking-methods-dynamically\">Example 3: Invoking Methods Dynamically<\/h3>\n\n\n\n<p>Reflection allows you to call methods at runtime.<\/p>\n\n\n\n<p>Java<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport java.lang.reflect.Method;\nclass Greeter {\n    public void sayHello(String name) {\n        System.out.println(&quot;Hello, &quot; + name + &quot;!&quot;);\n    }\n}\npublic class InvokeMethod {\n    public static void main(String&#x5B;] args) {\n        try {\n            Class&amp;lt;?&gt; greeterClass = Class.forName(&quot;Greeter&quot;);\n            Object greeterInstance = greeterClass.getDeclaredConstructor().newInstance(); \/\/ Create an instance\n\n            Method sayHelloMethod = greeterClass.getMethod(&quot;sayHello&quot;, String.class);\n            sayHelloMethod.invoke(greeterInstance, &quot;World&quot;); \/\/ Invoke the method\n\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/pre><\/div>\n\n\n<p>This code gets the <code>sayHello<\/code> method and then invokes it on a <code>Greeter<\/code> object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-4-accessing-and-modifying-private-fields\">Example 4: Accessing and Modifying Private Fields<\/h3>\n\n\n\n<p>You can even access and change private fields using reflection. Be careful with this, as it can break encapsulation.<\/p>\n\n\n\n<p>Java<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport java.lang.reflect.Field;\nclass Account {\n    private double balance;\n\n    public Account(double balance) {\n        this.balance = balance;\n    }\n\n    public double getBalance() {\n        return balance;\n    }\n}\npublic class AccessPrivateField {\n    public static void main(String&#x5B;] args) {\n        try {\n            Account myAccount = new Account(100.0);\n            System.out.println(&quot;Initial Balance: &quot; + myAccount.getBalance());\n\n            Field balanceField = Account.class.getDeclaredField(&quot;balance&quot;);\n            balanceField.setAccessible(true); \/\/ Allow access to private field\n\n            balanceField.set(myAccount, 200.0); \/\/ Set new value\n            System.out.println(&quot;New Balance: &quot; + myAccount.getBalance());\n\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/pre><\/div>\n\n\n<p>This example shows how to get a private field, make it accessible, and change its value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"when-to-use-and-when-to-avoid-reflection\">When to Use and When to Avoid Reflection<\/h2>\n\n\n\n<p>Use Reflection when:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You build frameworks that need to interact with user-defined classes.<\/li>\n\n\n\n<li>You need to inspect or modify code at runtime for debugging or testing.<\/li>\n\n\n\n<li>You create tools that analyze or generate code.<\/li>\n<\/ul>\n\n\n\n<p>Avoid Reflection when:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Performance is critical. Reflection operations are slower than direct calls.<\/li>\n\n\n\n<li>You can achieve the same result with normal object-oriented programming.<\/li>\n\n\n\n<li>It breaks encapsulation. Direct access to private members can make code harder to maintain and debug.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Reflection in Java allows a running Java program to examine or modify itself. It lets you inspect classes, interfaces, fields, and methods at runtime. You can also create new objects, invoke methods, and get or set field values dynamically.<\/p>\n","protected":false},"author":41,"featured_media":35712,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36826],"content_type":[],"class_list":["post-48283","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-java"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Reflection in Java with Examples<\/title>\n<meta name=\"description\" content=\"Learn Java Reflection and how the java.lang.reflect package allows dynamic class and method manipulation at runtime.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/reflection-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Reflection in Java with Examples\" \/>\n<meta property=\"og:description\" content=\"Learn Java Reflection and how the java.lang.reflect package allows dynamic class and method manipulation at runtime.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/reflection-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-10-28T06:47:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-07-03T14:32:35+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Java-Server-Pages.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1366\" \/>\n\t<meta property=\"og:image:height\" content=\"768\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/reflection-in-java\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/reflection-in-java\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Reflection in Java with Examples\",\"datePublished\":\"2021-10-28T06:47:15+00:00\",\"dateModified\":\"2025-07-03T14:32:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/reflection-in-java\\\/\"},\"wordCount\":606,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/reflection-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/06\\\/Java-Server-Pages.jpg\",\"keywords\":[\"java\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/reflection-in-java\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/reflection-in-java\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/reflection-in-java\\\/\",\"name\":\"Reflection in Java with Examples\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/reflection-in-java\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/reflection-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/06\\\/Java-Server-Pages.jpg\",\"datePublished\":\"2021-10-28T06:47:15+00:00\",\"dateModified\":\"2025-07-03T14:32:35+00:00\",\"description\":\"Learn Java Reflection and how the java.lang.reflect package allows dynamic class and method manipulation at runtime.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/reflection-in-java\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/reflection-in-java\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/reflection-in-java\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/06\\\/Java-Server-Pages.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/06\\\/Java-Server-Pages.jpg\",\"width\":1366,\"height\":768,\"caption\":\"Java Server Pages\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/reflection-in-java\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Reflection in Java with Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Reflection in Java with Examples","description":"Learn Java Reflection and how the java.lang.reflect package allows dynamic class and method manipulation at runtime.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mygreatlearning.com\/blog\/reflection-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Reflection in Java with Examples","og_description":"Learn Java Reflection and how the java.lang.reflect package allows dynamic class and method manipulation at runtime.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/reflection-in-java\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2021-10-28T06:47:15+00:00","article_modified_time":"2025-07-03T14:32:35+00:00","og_image":[{"width":1366,"height":768,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Java-Server-Pages.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/reflection-in-java\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/reflection-in-java\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Reflection in Java with Examples","datePublished":"2021-10-28T06:47:15+00:00","dateModified":"2025-07-03T14:32:35+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/reflection-in-java\/"},"wordCount":606,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/reflection-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Java-Server-Pages.jpg","keywords":["java"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/reflection-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/reflection-in-java\/","url":"https:\/\/www.mygreatlearning.com\/blog\/reflection-in-java\/","name":"Reflection in Java with Examples","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/reflection-in-java\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/reflection-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Java-Server-Pages.jpg","datePublished":"2021-10-28T06:47:15+00:00","dateModified":"2025-07-03T14:32:35+00:00","description":"Learn Java Reflection and how the java.lang.reflect package allows dynamic class and method manipulation at runtime.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/reflection-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/reflection-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/reflection-in-java\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Java-Server-Pages.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Java-Server-Pages.jpg","width":1366,"height":768,"caption":"Java Server Pages"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/reflection-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Reflection in Java with Examples"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Java-Server-Pages.jpg",1366,768,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Java-Server-Pages-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Java-Server-Pages-300x169.jpg",300,169,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Java-Server-Pages-768x432.jpg",768,432,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Java-Server-Pages-1024x576.jpg",1024,576,true],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Java-Server-Pages.jpg",1366,768,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Java-Server-Pages.jpg",1366,768,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Java-Server-Pages-640x768.jpg",640,768,true],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Java-Server-Pages-96x96.jpg",96,96,true],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/Java-Server-Pages-150x84.jpg",150,84,true]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"Reflection in Java allows a running Java program to examine or modify itself. It lets you inspect classes, interfaces, fields, and methods at runtime. You can also create new objects, invoke methods, and get or set field values dynamically.","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/48283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=48283"}],"version-history":[{"count":8,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/48283\/revisions"}],"predecessor-version":[{"id":109474,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/48283\/revisions\/109474"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/35712"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=48283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=48283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=48283"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=48283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}