0% found this document useful (0 votes)
84 views4 pages

Reflections: Reflection in Java Provides Ability To Inspect and Modify The Runtime Behavior

Java reflection allows inspection of classes, interfaces, fields and methods at runtime without knowing their names at compile time. It enables examination and modification of an object's structure and behavior at runtime. Reflection is commonly used by frameworks to examine or modify the runtime behavior of applications and is useful for tasks like dependency injection, method invocation, and testing. However, reflection has disadvantages like lower performance and security issues compared to normal programming.

Uploaded by

Praneeth Mutnuru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views4 pages

Reflections: Reflection in Java Provides Ability To Inspect and Modify The Runtime Behavior

Java reflection allows inspection of classes, interfaces, fields and methods at runtime without knowing their names at compile time. It enables examination and modification of an object's structure and behavior at runtime. Reflection is commonly used by frameworks to examine or modify the runtime behavior of applications and is useful for tasks like dependency injection, method invocation, and testing. However, reflection has disadvantages like lower performance and security issues compared to normal programming.

Uploaded by

Praneeth Mutnuru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Reflections

Java Reflection makes it possible to inspect classes, interfaces, fields and


methods at runtime, without knowing the names of the classes, methods etc. at
compile time. It is also possible to instantiate new objects, invoke methods and
get/set field values using reflection.

Reflection in java provides ability to inspect and modify the runtime behavior


of applications. Reflection is one of the advance topic of core java. Using
reflection we can inspect a class,interface, enums, get their structure, methods
and fields information at runtime even though class is not accessible at compile
time. We can also use reflection to instantiate an object, invoke it’s methods,
change field values.

Java Reflection Example


Here is a quick Java Reflection example to show you what using
reflection looks like:

Method[] methods = MyObject.class.getMethods();

for(Method method : methods){

System.out.println("method = " + method.getName());

This example obtains the Class object from the class called MyObject.


Using the class object the example gets a list of the methods in that
class, iterates the methods and print out their names.

Reflection enables us to:

 Examine an object's class at runtime


 Construct an object for a class at runtime
 Examine a class's field and method at runtime
 Invoke any method of an object at runtime
 Change accessibility flag of Constructor, Method and Field
 etc.
Reflection is the common approach of framework.

"Reflection is commonly used by programs which require the ability


to examine or modify the runtime behaviour of applications running
in the Java virtual machine." This concept is often mixed with
introspection. The following are their definitions from Wiki:

1. Introspection is the ability of a program to examine the type or


properties of an object at runtime.
2. Reflection is the ability of a program to examine and modify the
structure and behavior of an object at runtime.
From their definitions, introspection is a subset of reflection. Some
languages support introspection, but do not support reflection, e.g.,
C++.

Introspection Example: The instanceof operator determines whether


an object belongs to a particular class.
if(obj instanceof Dog){
Dog d = (Dog)obj;
d.bark();
}

Reflection Example: The Class.forName() method returns the Class


object associated with the class/interface with the given name(a string
and full qualified name). The forName method causes the class with
the name to be initialized.

// with reflection
Class<?> c = Class.forName("classpath.and.classname");
Object dog = c.newInstance();
Method m = c.getDeclaredMethod("bark", new Class<?>[0]);
m.invoke(dog);
In Java, reflection is more about introspection, because you can not
change structure of an object. There are some APIs to change
accessibilities of methods and fields, but not structures.

Reflection is a very powerful concept and it’s of little use in normal


programming but it’s the backbone for most of the Java, J2EE frameworks.
Some of the frameworks that use reflection are:

1. JUnit – uses reflection to parse @Test annotation to get the test methods
and then invoke it.
2. Spring – dependency injection, read more at Spring Dependency
Injection
3. Tomcat web container to forward the request to correct module by
parsing their web.xml files and request URI.
4. Eclipse auto completion of method names
5. Struts
6. Hibernate
We should not use reflection in normal programming where we already have
access to the classes and interfaces because of following drawbacks.

 Poor Performance – Since reflection resolve the types dynamically, it


involves processing like scanning the classpath to find the class to load,
causing slow performance.
 Security Restrictions – Reflection requires runtime permissions that
might not be available for system running under security manager. This can
cause you application to fail at runtime because of security manager.
 Security Issues – Using reflection we can access part of code that we are
not supposed to access, for example we can access private fields of a class
and change it’s value. This can be a serious security threat and cause your
application to behave abnormally.
 High Maintenance – Reflection code is hard to understand and debug,
also any issues with the code can’t be found at compile time because the
classes might not be available, making it less flexible and hard to maintain.

You might also like