Concept
Methods
A method is a named block of code that performs one task. It avoids duplication and organizes logic.
Where it fits
Packages: Metaprogramming (pkg17metaprogramming), Core Java (pkg1core), Design patterns (pkg8patterns)
Learn
Existing chapters for this concept.
- Methods
A method is a named block of code that performs one task. It avoids duplication and organizes logic.
Open lesson →
See it in code
Existing Java examples.
- core 7 Methods
- Java is ALWAYS pass-by-value. For objects, the VALUE passed is the reference (so you can mutate the object, but reassigning the param doesn't affect the…
Open example →
Prepare
Existing interview questions.
- What is the diamond problem and how does Java handle default methods?
Conflict when two interfaces provide the same default method.
Open question → - Can interface methods be private?
Clear naming, small methods, immutability, proper exceptions, tests, and idiomatic APIs.
Open question → - Generic method syntax?
Use bounded wildcards on input params (PECS), exact types on outputs.
Open question → - getMethod vs getDeclaredMethod?
includes inherited public; all declared in class (any visibility).
Open question → - MethodHandles vs reflection?
MethodHandles are JVM-aware, faster, type-safe; preferred for repeated invocation.
Open question → - Factory method intent?
Match the problem; prefer the simplest solution that communicates intent.
Open question → - Prefer static factory methods over constructors?
Named methods ( , ) clarify intent; can cache, return subtypes.
Open question →
Java versions
- Method references
Method references. A method reference points at an existing method or constructor where a functional interface is required.
Open version note → - Default and static interface methods
Default and static interface methods. An interface can carry a default instance method and a static method. That is how Collection gained stream() without…
Open version note → - Convenience Factory Methods for Collections
Convenience Factory Methods for Collections. List.of, Set.of, and Map.of build immutable collections and reject null elements.
Open version note → - Unnamed Classes and Instance Main Methods (Preview)
Unnamed Classes and Instance Main Methods (Preview). A small program can omit the class wrapper and use an instance main method. This is a preview.
Open version note → - Implicitly Declared Classes and Instance Main Methods (Second Preview)
Implicitly Declared Classes and Instance Main Methods (Second Preview). The compact-program preview continues. A source file can still omit the usual class…
Open version note → - Implicitly Declared Classes and Instance Main Methods (Third Preview)
Implicitly Declared Classes and Instance Main Methods (Third Preview). The compact-program preview continues.
Open version note → - Deprecate the Memory-Access Methods in sun.misc.Unsafe for Removal
Deprecate the Memory-Access Methods in sun.misc.Unsafe for Removal. The memory-access methods of sun.misc.Unsafe are deprecated for removal. They are not…
Open version note → - Simple Source Files and Instance Main Methods (Fourth Preview)
Simple Source Files and Instance Main Methods (Fourth Preview). The compact-program preview continues under the name simple source files.
Open version note → - Compact Source Files and Instance Main Methods
Compact Source Files and Instance Main Methods. Beginners can write a small program without the declarations used for large programs. The same language and…
Open version note → - Ahead-of-Time Method Profiling
Ahead-of-Time Method Profiling. Method-execution profiles from a training run can be stored in the AOT cache, so the JIT can compile hot methods sooner when…
Open version note → - JFR Method Timing & Tracing
JFR Method Timing & Tracing. JDK Flight Recorder can time and trace selected methods by instrumenting bytecode. The application source does not have to change.
Open version note →