The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Reflection in details and how to get the metadata information of the class | Reflection in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
Java Reflection in details and how to get the metadata information of the class | Reflection in java Posted: Nov 8, 2017 8:16 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: Java Reflection in details and how to get the metadata information of the class | Reflection in java
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

Click here to watch in Youtube :
https://www.youtube.com/watch?v=dmDPT7Y22OM&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Reflection in details and how to get the metadata information of the class | Reflection in java

Java Reflection in details and how to get the metadata information of the class | Reflection in java
Java Reflection in details and how to get the metadata information of the class | Reflection in java

Display.java
class Display
{
private String str;

public Display()
{
str = "Welcome";
}

public void method1()
{
System.out.println("Inside method1,The string is " + str);
}

public void method2(int n)
{
System.out.println("\nInside method2,The number is " + n);
}

private void method3()
{
System.out.println("Private method invoked");
}
}
ReflectionDemo.java
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/*
* A simple Java program to demonstrate the use of reflection.
*/

public class ReflectionDemo
{
public static void main(String[] args) throws Exception
{
/*
* Creating object whose property is to be checked
*/

Display displayObj = new Display();

/*
* Creating class object from the object using getclass method
*/

Class<? extends Display> classObj = displayObj.getClass();
System.out.println("The name of class is " + classObj.getName());

/*
* Getting the constructor of the class through the object of the class
*/

Constructor<? extends Display> constructor = classObj.getConstructor();
System.out.println("The name of constructor is " + constructor.getName());

System.out.println("\nThe public methods of class are : ");

/*
* Getting methods of the class through the object of the class by using
* getMethods
*/

Method[] methodArray = classObj.getMethods();

/*
* Printing method names
*/

for (Method method : methodArray)
{
System.out.println(method.getName());
}

/*
* Creates object of desired method by providing the method name and
* parameter class as arguments to the getDeclaredMethod
*/

Method method2Obj = classObj.getDeclaredMethod("method2", int.class);

/*
* invokes the method at runtime
*/

method2Obj.invoke(displayObj, 19);

/*
* Creates object of the desired field by providing the name of field as
* argument to the getDeclaredField method
*/

Field field = classObj.getDeclaredField("str");

/*
* Allows the object to access the field irrespective of the access
* specifier used with the field
*/

field.setAccessible(true);

/*
* Takes object and the new value to be assigned to the field as
* arguments
*/

field.set(displayObj, "JAVA");

/*
* Creates object of desired method by providing the method name as
* argument to the getDeclaredMethod
*/

Method method1Obj = classObj.getDeclaredMethod("method1");

/*
* invokes the method at runtime
*/

method1Obj.invoke(displayObj);

/*
* Creates object of the desired method by providing the name of method
* as argument to the getDeclaredMethod method
*/

Method method3Obj = classObj.getDeclaredMethod("method3");

/*
* Allows the object to access the method irrespective
* of the access specifier used with the method
*/

method3Obj.setAccessible(true);

/*
* invokes the method at runtime
*/

method3Obj.invoke(displayObj);
}

}
Output
The name of class is Display
The name of constructor is Display

The public methods of class are :
method2
method1
wait
wait
wait
equals
toString
hashCode
getClass
notify
notifyAll

Inside method2,The number is 19
Inside method1,The string is JAVA
Private method invoked

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/ReflectionDemo_Intro_2_details.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ReflectionDemo_Intro_2_details

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/7b0b4fbacd011a10dc23b42c4acc3ae6988db782/BasicJava/ReflectionDemo_Intro_2_details/?at=master

See also:

  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Read: Java Reflection in details and how to get the metadata information of the class | Reflection in java

    Topic: Java and Natural Language Processing Previous Topic   Next Topic Topic: Java basic interview questions for freshers

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use