SlideShare a Scribd company logo
2
Most read
3
Most read
9
Most read
Java Reflection
Agenda
What is Reflection?
How to get the object Class class
Reflection vs Metaprogramming
Why do we need reflection?
Demo
Real Usage Examples
What is Reflection?
Reflection is used for observing and modifying program execution at runtime.
A reflection-oriented program component can monitor the execution of an
enclosure of code and can modify itself according to a desired goal related to that
enclosure.
Reflection is one of those things like multi-threading where everyone with experience of
it says “Don’t use it unless you absolutely have to”.
Example:
// without reflection
Foo foo = new Foo();
foo.hello();
// with reflection
Object foo = Class.forName("complete.classpath.and.Foo").newInstance();
// Alternatively: Object foo = Foo.class.newInstance();
Method m = foo.getClass().getDeclaredMethod("hello", new Class<?>[0]);
m.invoke(foo);
How to get the object of Class class
forName() method of Class class
Class c=Class.forName("Simple")
System.out.println(c.getName())
getClass() method of Object class
Class c=obj.getClass();
System.out.println(c.getName());
the .class syntax
Class c2 = Test.class;
System.out.println(c2.getName());
Reflection vs Metaprogramming
Metaprogramming is "programs which write programs". This includes programs
that read the text of programs (arguably including themselves but that is rather
rare), analyze that code, and make changes.
Reflection is the ability for a program to inquire about its own structure.
Why do we need reflection?
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
Demo
Real Usage Example
Code analyzer tools
Eclipse (Other IDEs) auto completion of method names
Spring Framework for creating the beans
Parsing annotations by ORMs like hibernate entity
Somewhere I read this quote:
“When you will need reflection; you will know it”.
For live project example
https://github.com/NexThoughts/ReflectionDemo
References
http://tutorials.jenkov.com/java-reflection/index.html
http://www.javatpoint.com/java-reflection
http://www.programmerinterview.com/index.php/java-questions/java-reflection-
example/
http://howtodoinjava.com/core-java/annotations/complete-java-annotations-
tutorial/
http://howtodoinjava.com/core-java/related-concepts/real-usage-examples-of-
reflection-in-java/
Thanks You

More Related Content

PDF
Java 8 Lambda Expressions
PDF
REST APIs with Spring
PDF
Java 8 Lambda Built-in Functional Interfaces
PDF
Lambda Expressions in Java
PPTX
Functional programming with Java 8
PPTX
Java 8 lambda
PPTX
Spring boot
PPTX
Spring Boot and REST API
Java 8 Lambda Expressions
REST APIs with Spring
Java 8 Lambda Built-in Functional Interfaces
Lambda Expressions in Java
Functional programming with Java 8
Java 8 lambda
Spring boot
Spring Boot and REST API

What's hot (20)

PDF
Java 8 Lambda Expressions & Streams
PDF
Spring MVC Framework
PPTX
Introduction to Spring Boot
PPTX
Java 8 Lambda and Streams
PDF
JPA and Hibernate
PPT
Java Persistence API (JPA) Step By Step
PPTX
Java 8 streams
PDF
Hibernate complete notes_by_sekhar_sir_javabynatara_j
PPTX
Spring mvc
PPTX
Introduction to Node js
PPTX
Object Oriented Programing JAVA presentaion
PDF
JavaScript Programming
PDF
Spring MVC
PPT
Spring Framework
PDF
Spring Framework - Core
PDF
Wpf Introduction
PPTX
Hibernate ppt
PPTX
Spring data jpa
PPT
Java 8 Streams
PPTX
Spring boot - an introduction
Java 8 Lambda Expressions & Streams
Spring MVC Framework
Introduction to Spring Boot
Java 8 Lambda and Streams
JPA and Hibernate
Java Persistence API (JPA) Step By Step
Java 8 streams
Hibernate complete notes_by_sekhar_sir_javabynatara_j
Spring mvc
Introduction to Node js
Object Oriented Programing JAVA presentaion
JavaScript Programming
Spring MVC
Spring Framework
Spring Framework - Core
Wpf Introduction
Hibernate ppt
Spring data jpa
Java 8 Streams
Spring boot - an introduction
Ad

Viewers also liked (20)

PPTX
Grails custom tag lib
PPT
Bootcamp linux commands
PDF
Grails internationalization-160524154831
PPTX
Meta Programming in Groovy
ODP
PPTX
Introduction to mongo db
PPTX
PDF
Unit test-using-spock in Grails
PPTX
Actors model in gpars
PPTX
MetaProgramming with Groovy
PPT
Grails Controllers
PPTX
Grails services
PPTX
Grails with swagger
PPTX
ODP
Command objects
Grails custom tag lib
Bootcamp linux commands
Grails internationalization-160524154831
Meta Programming in Groovy
Introduction to mongo db
Unit test-using-spock in Grails
Actors model in gpars
MetaProgramming with Groovy
Grails Controllers
Grails services
Grails with swagger
Command objects
Ad

Similar to Java reflection (20)

PPT
Reflection in java
PPTX
Reflection in Java
PPTX
Java Reflection Concept and Working
PPTX
Java Reflection @KonaTechAdda
PDF
Java Reflection Explained Simply
PDF
Understanding And Using Reflection
PPTX
Cuối cùng cũng đã chuẩn bị slide xong cho seminar ngày mai. Mai seminar đầu t...
PDF
Reflection and Introspection
PDF
Pharo: a reflective language A first systematic analysis of reflective APIs
PDF
Metaprograms and metadata (as part of the the PTT lecture)
PPT
Reflection
PDF
Advanced Reflection in Pharo
PPT
10 reflection
PDF
Scala reflection
PDF
Java reflection
PDF
Антон Бикинеев, Reflection in C++Next
PDF
Reflections the most important feature in java
PDF
Pharo: A Reflective System
PDF
Java Reflection
PDF
Dynamic Proxy by Java
Reflection in java
Reflection in Java
Java Reflection Concept and Working
Java Reflection @KonaTechAdda
Java Reflection Explained Simply
Understanding And Using Reflection
Cuối cùng cũng đã chuẩn bị slide xong cho seminar ngày mai. Mai seminar đầu t...
Reflection and Introspection
Pharo: a reflective language A first systematic analysis of reflective APIs
Metaprograms and metadata (as part of the the PTT lecture)
Reflection
Advanced Reflection in Pharo
10 reflection
Scala reflection
Java reflection
Антон Бикинеев, Reflection in C++Next
Reflections the most important feature in java
Pharo: A Reflective System
Java Reflection
Dynamic Proxy by Java

More from NexThoughts Technologies (20)

PDF
PDF
Docker & kubernetes
PDF
Apache commons
PDF
Microservice Architecture using Spring Boot with React & Redux
PDF
Solid Principles
PDF
Introduction to TypeScript
PDF
Smart Contract samples
PDF
My Doc of geth
PDF
Geth important commands
PDF
Ethereum genesis
PPTX
Springboot Microservices
PDF
An Introduction to Redux
PPTX
Google authentication
Docker & kubernetes
Apache commons
Microservice Architecture using Spring Boot with React & Redux
Solid Principles
Introduction to TypeScript
Smart Contract samples
My Doc of geth
Geth important commands
Ethereum genesis
Springboot Microservices
An Introduction to Redux
Google authentication

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Encapsulation theory and applications.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Approach and Philosophy of On baking technology
PDF
Machine learning based COVID-19 study performance prediction
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
KodekX | Application Modernization Development
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Cloud computing and distributed systems.
PPTX
Big Data Technologies - Introduction.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
“AI and Expert System Decision Support & Business Intelligence Systems”
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Encapsulation theory and applications.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
The AUB Centre for AI in Media Proposal.docx
Building Integrated photovoltaic BIPV_UPV.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Approach and Philosophy of On baking technology
Machine learning based COVID-19 study performance prediction
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Encapsulation_ Review paper, used for researhc scholars
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Chapter 3 Spatial Domain Image Processing.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
KodekX | Application Modernization Development
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Cloud computing and distributed systems.
Big Data Technologies - Introduction.pptx

Java reflection