Learn Reflection with Java

Learn Reflection with Java

4h 9mIntermediate2024-10-23

Authors

Pearson

Pearson

Heinz Kabutz

Heinz Kabutz

Course details

Reflection is a powerful feature in Java that can be used to expand the capabilities of a program, or to examine a class, method, or object at runtime, even if you don’t know its name. In this course from Pearson, get started with the basics, then learn how reflection works with the more modern language features such as sealed classes and records, as well as how arrays can be manipulated and created via reflection. Along the way, explore the purpose of MethodHandles and VarHandles to find out when and how to use them both like a pro.

Learning objectives
Inspect a class and find methods, fields, and constructors at runtime.
Call methods and change fields, even if they are private.
Write more general code that can be reused in many different contexts.
Understand VarHandles and MethodHandles and know when to use them.
Inspect sealed classes and records via reflection.

Skills covered

JavaProgramming FoundationsOracleProgramming LanguagesSoftware DevelopmentOne-Off

Concepts

Introduction

  • Learn reflection with Java - Introduction

Lesson 1 - Introduction to Reflection

  • Learning objectives
  • Who am I
  • Why learn reflection
  • Class class
  • Method
  • Modifier and access flag
  • Constructor
  • Generics
  • Nested classes
  • Sealed classes
  • Records
  • Exercise

Lesson 2 - Deep Reflection

  • Learning objectives
  • Making private members accessible
  • Performance considerations

Lesson 3 - Arrays

  • Learning objectives
  • Accessing elements
  • Creating new
  • Exercise

Lesson 4 - java.lang.invoke

  • Learning objectives
  • MethodHandles.lookup
  • privateLookupIn()
  • MethodHandle
  • MethodType
  • VarHandle
  • Unreflecting
  • Exercises

Summary

  • Learn reflection with Java - Summary
100,000 Toman