What is the purpose of the java.lang.reflect package?
Learn what java.lang.reflect does: inspect classes, invoke methods, build dynamic proxies, and handle modules, records and performance concerns.
2,889 posts about Java: questions with answers, tutorials and articles, newest first.
Learn what java.lang.reflect does: inspect classes, invoke methods, build dynamic proxies, and handle modules, records and performance concerns.
Use getDeclaredField and setAccessible(true) to read or write private fields. Covers module restrictions, superclass fields, final fields and VarHandle alternatives.
Reflection lets Java code inspect and invoke classes, fields, methods and annotations at runtime. Learn the API, module restrictions and alternatives.
@Retention sets how long an annotation lives, @Target sets where it may be used. See all policies, element types and a reflection example.
Declare a custom annotation with @interface, set retention and target, and read it through reflection. Covers repeatable annotations, field validation and compile-time processors.
A Java annotation is metadata attached to code and read by the compiler, tools or frameworks. Learn built-in annotations and how to create custom ones.
Give Java enums fields, constructors and methods, override behaviour per constant, implement interfaces and avoid common pitfalls such as persisting ordinals.
Enums define a fixed set of type-safe constants in Java. Learn fields, methods, switch expressions, EnumSet and EnumMap with working examples.
The Java Character class wraps the primitive char and provides Unicode-aware methods to classify and convert characters. Learn its methods, code points and common pitfalls.
Use Integer.parseInt for a primitive int or Integer.valueOf for a boxed value. Learn to handle NumberFormatException, whitespace, radixes and large numbers.
String.intern() returns a canonical string from the JVM pool so equal strings share one object. See when it saves memory, its costs and better options.
== compares primitives by value and objects by reference, while .equals() compares content when overridden. See the string pool and Integer cache traps.
Compare the ways to concatenate strings in Java, from the plus operator to StringBuilder, String.join and text blocks, and learn when each one is best.
System.gc() only asks the JVM to collect garbage and often forces a full pause. Learn why to avoid it, where it is still called, and better options.
System.gc() is only a hint the JVM may ignore. Release references, close resources, use weak references and tune the collector instead of forcing runs.
finalize() once let objects clean up before garbage collection, but it is deprecated for removal since Java 18. Use AutoCloseable and Cleaner instead.
Java offers Serial, Parallel, G1, ZGC and Shenandoah collectors. Learn how each works, when to use it, and why CMS no longer exists.
The JVM interprets bytecode, profiles it, and compiles hot code to native code at run time. Learn tiered C1 and C2 compilation and key optimizations.
The JDK contains the JRE, and the JRE contains the JVM. See what each one does, how they work together and what to install today.
Walk through each stage of Java bytecode execution in the JVM, from class loading and verification to tiered JIT compilation and garbage collection.