Running on Java 22-ea+27-2262 (Preview)
Home of The JavaSpecialists' Newsletter

java.lang.reflect.* Course

Write Dynamically Reusable Code in Java

Using Java 17

Duration of 1/2 Day


As a Java programmer, if we know how reflection works, we can save a lot of time and effort. Instead of writing versions of the same code over and over again, we can write a single method that does what we want in a general way. In this course we start with the basics, then show how reflection works with the more modern language features such as sealed classes and records. We also show how arrays can be manipulated and created via reflection. Lastly, we show MethodHandle and VarHandle. Learn from hands-on exercises that are followed by demos revealing the solutions.

Benefits of Course

  • Save programming time by writing dynamic solutions to reoccurring problems.
  • Learn how dynamic coding via reflection affects Java performance and correctness.
  • A well-rounded Java programmer should know reflection, when to use it, and more importantly, when not to use it.

What you'll learn and how you can apply it

By the end of the course, you'll understand:

  • Basic reflection: Class, Method, Field, Constructor
  • More advanced reflection concepts: generics, records, sealed classes, nested classes, modules
  • Deep reflection in Java, which allows changes to private final fields
  • How to manipulate arrays with reflection
  • MethodHandle and VarHandle

And you'll be able to:

  • Inspect a class and find methods, fields and contructors at runtime
  • Call methods, 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
  • Know how sealed classes and records can be inspected via reflection

This live event is for you because...

  • You want to be considered a guru by your colleagues and peers, writing "magical" Java code that dynamically adapts itself to change.
  • You want to save time in your development by writing highly reusable code.
  • You are in the top tier of developers and want to learn how to write libraries and frameworks

Who should take this course?

  • Students should be very familiar with Java 11, understanding data structures, object orientation, recursion and modern Java syntax.
  • Prior experience with reflection is a bonus, but not necessary.

Related Newsletters

  • Issue 298 Finding Permitted Subclasses
    Sealed classes show us which subclasses they permitted. Unfortunately there is no way to do this recursively. In this newsletter we show how to navigate the sealed class hierarchy and write a ClassSpliterator that can then create a Stream of classes.
  • Issue 291 Snakes and Ladders
    "Would you like to play with me?" - Summer holidays are a good time to relax and whip out a board game or two. In this newsletter we explore a children's favourite - snakes and ladders, with a simulation written in Java.
  • Issue 287 @PolymorphicSignature
    MethodHandles annotate the invoke() methods with @PolymorphicSignature. In this newsletter we see how this can help to avoid unnecessary object creation.
  • Issue 275 EnhancedStream with Dynamic Proxy
    In our previous newsletter we enhanced Java 8 Streams by decorating them with an EnhancedStream class. The code had a lot of repetition, which often leads to bugs if written by hand. In this newsletter we use a dynamic proxy to create an Enhance...
  • Issue 274 EnhancedStream
    Java 8 Streams have a rather narrow-minded view of what makes objects distinct. In this newsletter, we enhance Streams to allow a more flexible approach for deciding uniqueness.
  • Issue 273 Truly Public Methods
    Class.getMethods() returns all the public methods in this class and the classes in its ancestry. However, we cannot necessarily call these methods, for example if they are declared in a private inner class or a lambda. In this newsletter we atte...
  • Issue 272 Hacking Enums Revisited
    In a previous newsletter, we looked at how we could dynamically add new enums and also rewire affected switch statements. Due to improvements in Java security, our old approach needs to be updated for Java 12.
  • Issue 270 Excursions into Deduplication
    When are Strings deduplicated? How can we find out whether they are of any benefit in our application? How much does it cost in terms of CPU? We try to show you how we can get this from the deduplication statistics provided by the JVM.
  • Issue 254 Big O Cost of Class.getMethod()
    We now look at why the best-case scenario for a getMethod() call is O(n), not O(1) as we would expect. We also discover that the throughput of getMethod() has doubled in Java 9.
  • Issue 249 @Contended @since 9
    Java 9 is more strict about what system internal classes we can use. So how can we use @Contended in Java 9? This article shows you how.
  • Issue 242 Concurrency Puzzle Explained, Solved With StampedLock
    Most programmers had a blind spot with the statement "arr[size++] = e;" We somehow think that size will be updated after the assignment. In this newsletter we look at this basic concurrency bug and also present a solution in the form of the Java...
  • Issue 237 String Compaction
    Java 6 introduced a mechanism to store ASCII characters inside byte[] instead of a char[]. This feature was removed again in Java 7. However, it is coming back in Java 9, except this time round, compaction is enabled by default and byte[] is alw...
  • Issue 235 Checking HashMaps with MapClashInspector
    Java 8 HashMap has been optimized to avoid denial of service attacks with many distinct keys containing identical hash codes. Unfortunately performance might degrade if you use your own keys. In this newsletter we show a tool that you can use to...
  • Issue 229 Cleaning ThreadLocals
    ThreadLocals should in most cases be avoided. They can solve some tough problems, but can introduce some nasty memory leaks, especially if the ThreadLocal class or value refer to our own classes, not a system class. In this newsletter we show so...
  • Issue 228 Extracting Real Task from FutureTask
    ExecutorService allows us to submit either Callable or Runnable. Internally, this is converted to a FutureTask, without the possibility of extracting the original task. In this newsletter we look at how we can dig out the information using refle...
  • Issue 185 Book Review: Java: The Good Parts
    In his latest book, Jim Waldo describes several Java features that he believes make Java "good". A nice easy read, and I even learned a few new things from it.
  • Issue 175 Creating Objects Without Calling Constructors
    De-Serialization creates objects without calling constructors. We can use the same mechanism to create objects at will, without ever calling their constructors.
  • Issue 168 The Delegator
    In this newsletter we show the reflection plumbing needed for writing a socket monitor that sniffs all the bytes being sent or received over all the Java sockets. The Delegator is used to invoke corresponding methods through some elegant guesswork.
  • Issue 167 Annotation Processing Tool
    In this newsletter we answer the question: "How do we force all subclasses to contain a public no-args constructor?" The Annotation Processing Tool allows us to check conditions like this at compile time, rather than only at runtime.
  • Issue 161 Of Hacking Enums and Modifying "final static" Fields
    The developers of the Java language tried their best to stop us from constructing our own enum instances. However, for testing purposes, it can be useful to temporarily add new enum instances to the system. In this newsletter we show how we can ...
  • Issue 144 Book Review: Java Puzzlers
    Experienced Java programmers will love the Java Puzzlers book by Josh Bloch and Neal Gafter, both well known Java personalities. In this newsletter, we look at two of the puzzles as a teazer for the book.
  • Issue 142 Instrumentation Memory Counter
    Memory usage of Java objects has been a mystery for many years. In this newsletter, we use the new instrumentation API to predict more accurately how much memory an object uses. Based on earlier newsletters, but revised for Java 5 and 6.

java.lang.reflect.* Course Enquiry


Prerequisites

This course is aimed at intermediate to advanced Java programmers wanting to learn what tricks java.lang.reflect has to offer and how to best use them.

Pricing Options

We have several options for you to join this course:

1. Virtual In-house Course:

Presented via video conference to your team of programmers by the author of the course. Price is €2233 for up to 10 students, above that is an additional €198 per student.

  • Example 1: Course with 8 students taught remotely via video conference, price is €2233. Price per student is €279.
  • Example 2: Course with 12 students taught remotely via video conference, price is €2629. Price per student is €219.
  • Example 3: Course with 24 students taught remotely via video conference, price is €5005. Price per student is €208.

Please contact us if you have any questions.

2. In-person In-house Course:

Presented at your company in-person by one of our Certified JavaSpecialist Instructors. Price is €3289 for up to 10 students, above that is an additional €295 per student, plus the travel expenses of the instructor. Note that for in-person in-house courses, we need a minimum of three consecutive training days.

  • Example 1: Course with 8 students taught on-site at your company, price is €3289. Price per student is €411.
  • Example 2: Course with 12 students taught on-site at your company, price is €3879. Price per student is €323.
  • Example 3: Course with 18 students taught on-site at your company, price is €5649. Price per student is €313.

Please contact us if you have any questions.

2. Open Enrollment Classroom Course:

We occasionally offer this course as a classroom course in Chania on the Island of Crete. Price for the course is €695 per student.

We also offer this course as an open enrollment live remote course that you can attend from anywhere. Price is €469 per student.

Please contact us if you have would like to make a booking or if you have any questions.

4. Self-Paced Course:

This course is currently not available as a self-paced course.

Please contact us if you have any questions.

* Prices exclude EU VAT and withholding taxes where applicable. Please contact us for an exact quote for your country.

Open Courses

All our courses are offered as in-house courses. Please contact us on heinz@javaspecialists.eu.

* Price is excluding EU VAT where applicable. Please contact us for an exact quote for your country.

Detailed Outline

The emphasis of this training workshop is to learn what java.lang.reflect has to offer. Here is our outline:

1: Introduction to Reflection
  • Welcome
  • Why learn reflection?
  • Class Class
    • Method
    • Constructor
    • Generics
  • Nested classes
  • Sealed classes
  • Records
2: Deep Reflection
  • Making private members accessible
  • Performance considerations
3: Arrays
  • Component types
  • Accessing elements
  • Creating new
4: java.lang.invoke
  • MethodHandles.Lookup
    • privateLookupIn()
  • MethodHandle
    • MethodType
    • Finding constructors and methods
    • Invoking MethodHandle
    • Spreader
    • invokeExact()
  • VarHandle
  • Unreflecting
5: Conclusion

About the Author

Heinz Kabutz Java Conference Speaker

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Superpack '23

Superpack '23 Our entire Java Specialists Training in one huge bundle more...

Free Java Book

Dynamic Proxies in Java Book

110% Money-back Guarantee

Should you not be satisfied with the quality of the training or the delivery, we will gladly refund you 100% of the course fees. This needs to be brought to our attention within the first 4 hours of the course and a chance should be given to correct whatever you are not satisfied with. If you are still not satisfied, we will refund you 100% of the course fees, plus we will pay our own travel expenses. The training material then remains the property of JavaSpecialists.EU.


Cancellation Policy

If the course is cancelled more than two weeks before the start of the course, a 10% cancellation fee of the fees will apply, plus any non-refundable travel expenses incurred by the trainer.

If the course is cancelled within two weeks of the start of the course, a 50% cancellation fee of the fees will apply, plus any non-refundable travel expenses incurred by the trainer.

No refund will be given to cancellations during the course.


Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...