25. Advanced Topics (Optional for Intermediate Level)

This chapter is marked as optional for intermediate level, but trust me — these topics are extremely important in real-world Java development (especially in 2026). Many companies ask about them in interviews, and they appear in almost every large-scale project.

We’re going to cover:

  • Annotations
  • Reflection
  • Enums
  • Inner classes (static, non-static, anonymous)
  • Garbage Collection
  • Java Modules (Java 9+)

As always, we’ll go super slowly, with real-life analogies, complete runnable programs, step-by-step explanations, tables, common mistakes, and tons of examples you can copy-paste and run right now.

Let’s dive in — grab your cutting chai! ☕

1. Annotations

Annotations are metadata — extra information attached to classes, methods, fields, etc. They don’t directly affect program execution, but tools/frameworks (like Spring, Hibernate, JUnit) read them and behave differently.

Syntax: Start with @ symbol.

Built-in annotations you use every day:

Annotation Purpose Example Usage
@Override Tells compiler: this method overrides a superclass method @Override public void run()
@Deprecated Marks a method/class as outdated @Deprecated public void oldMethod()
@SuppressWarnings Tells compiler to ignore specific warnings @SuppressWarnings(“unchecked”)
@FunctionalInterface Marks an interface as functional (one abstract method) @FunctionalInterface interface MyFunc {}

Custom Annotation Example (very common in frameworks)

Java

Using it:

Java

Reading annotations at runtime (reflection — next topic):

Java

2. Reflection

Reflection allows you to inspect and modify classes, methods, fields, etc. at runtime — even private ones!

Use cases:

  • Frameworks (Spring, Hibernate) use reflection to auto-configure beans
  • JUnit uses it to find @Test methods
  • Serialization, dependency injection, dynamic proxies

Important classes:

  • Class
  • Field
  • Method
  • Constructor

Example: Inspecting a class at runtime

Java

Student class for above example:

Java

Warning: Reflection is slow and breaks encapsulation — use only when necessary (frameworks, testing, dynamic behavior).

3. Enums

Enums are special classes that represent a fixed set of constants.

Syntax:

Java

Features:

  • Type-safe
  • Can have fields, methods, constructors
  • Can implement interfaces

Advanced Enum Example (with fields & methods)

Java

Usage:

Java

4. Inner Classes (Static, Non-static, Anonymous)

Inner classes are classes defined inside another class.

Type Syntax / Declaration Can access outer fields? Static? Use Case
Non-static (Inner) class Inner { … } Yes (even private) No Helper class tightly coupled to outer
Static Nested static class StaticNested { … } Only static fields Yes Utility/helper class inside outer
Local Inside method: class Local { … } Yes (effectively final) No Very short-lived helper
Anonymous new SuperClass() { … } or new Interface() { … } Yes (effectively final) No One-time use (listeners, comparators)

Example: All Types

Java

5. Garbage Collection (GC) – How Java Cleans Memory

Java has automatic memory management — you never manually free memory (no delete like C++).

Garbage Collector (GC) automatically reclaims memory from objects that are no longer reachable.

Key Concepts:

  • Heap → where objects live
  • Garbage → objects with no live references
  • Reachability → object is reachable if there is a path from GC roots (stack variables, static fields, etc.)

Common GC Algorithms (you don’t need to know deeply):

  • Serial GC → single thread
  • Parallel GC → multiple threads
  • G1 GC → default in Java 11+ (region-based, low pause)
  • ZGC / Shenandoah → ultra-low pause (Java 15+)

How to help GC:

  • Set objects to null when no longer needed
  • Use try-with-resources for closeable resources
  • Avoid huge object graphs

Example:

Java

6. Java Modules (Java 9+ – Project Jigsaw)

Java 9 introduced the module system — a way to structure large applications with strong encapsulation and explicit dependencies.

module-info.java (the module descriptor)

Java

Benefits:

  • Strong encapsulation — private packages hidden
  • Better security
  • Smaller runtime (jlink → custom JRE)

Real-world: Spring Boot 3+ uses modules heavily.

Quick Recap Table (Your Cheat Sheet)

Topic Key Takeaway / Best Practice Example
Annotations Metadata for frameworks/compilers @Override, @MyLog
Reflection Runtime inspection/modification Class.forName(), getMethod()
Enums Type-safe constants + fields/methods enum Day { MONDAY }
Inner Classes Nested classes — static, non-static, anonymous class Inner {} vs static class Nested {}
Garbage Collection Automatic memory cleanup obj = null; + System.gc() (hint only)
Java Modules Strong encapsulation & dependencies (Java 9+) module-info.java

Homework for You (Practice to Master!)

  1. Annotations Create custom @LogExecutionTime annotation → use reflection to measure method execution time.
  2. Reflection Write a method that prints all fields and methods of any object passed to it.
  3. Enums Create enum Level { LOW, MEDIUM, HIGH } with int value() and fromValue(int) methods.
  4. Inner Classes Create an anonymous Comparator to sort list of strings by length.
  5. Modules Create two modules: com.webliance.core and com.webliance.app → make app require core.

You’ve completed the entire Core Java journey — congratulations! 🎉 You now know everything from basics to advanced topics — you’re ready for real-world projects, Spring Boot, interviews, and open-source contributions.

What would you like next?

  • Deep dive into Spring Boot?
  • Advanced topics (Concurrency, JVM internals, Design Patterns)?
  • Solve interview questions together?
  • Debug any code you wrote?

Just tell me — I’m right here with another cup of cutting chai! ☕🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *