Anonymous classes in Java
An anonymous class lets you define an interface implementation or a subclass and create an instance of it in the same expression.
CKTIS / Java
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
An anonymous class lets you define an interface implementation or a subclass and create an instance of it in the same expression.
An array initializer creates an array from values enclosed in braces. The number of entries determines its length.
Declare and create arrays, read and replace elements by index, and distinguish array length from list size. See what `new List[2]` creates and how to avoid raw `List` in new code.
A `.class` literal gives you the object that represents a named type. Use it to filter values, specify an `EnumMap` key type, select a monitor, or configure a field updater.
A lambda can capture a local variable without the `final` modifier if it is effectively final. A fixed reference does not make the object it points to immutable.
In Java SE 21, an `enum` constant can pass arguments to the enum constructor and have its own class body to define its behavior.
Bounds restrict which types can be used as generic arguments. Learn how to preserve a builder’s concrete type, compare values through a supertype, and require several bounds at once.
Learn why `Spliterator<String>.tryAdvance` can take a consumer of `String` or a consumer of a supertype such as `CharSequence`.
A hexadecimal integer literal expresses a value in base 16 using the prefix 0x or 0X. The suffix L selects long; without a suffix, the literal has type int.
An inner member class is tied to a specific instance of its enclosing class and can read that object's private fields.
Write `10000000001L` to make a `long` literal. Without the suffix, the value is too large for `int`, even if the destination variable is declared `long`.
Learn when an integer literal is an `int` or a `long`, how to write large values, and why `0L` is useful for running totals and conversions to `Long`.
`0L` is zero of type `long`, while `0` is zero of type `int`. That difference matters for overload selection, arithmetic, and defaults that must be `Long`.
A local class is a named nested class declared, for example, in a method block. The scope of its name does not limit the lifetime of its instances.
Method overloading lets several methods share a name while using different parameter lists. The compiler selects an overload using the arguments and their compile-time types, rather than the runtime classes of argument objects.
The catch (A | B e) syntax handles different exception types in one block. Alternative types must not have a subtype relationship, and the parameter e is implicitly final.
Distinguish static nested classes from inner classes, expose builder steps through public interfaces, and give a nested LRU cache its own type parameters.
Raw `List` and `ArrayList` bypass compile-time element-type checks. Assigning a raw list to `List<String>` neither validates its contents nor recovers a type argument.
In Java SE 21, a record’s compact canonical constructor validates or normalizes arguments before Java assigns their values to the component fields.
A bound such as `T extends Base<T>` lets base-class methods return a concrete subclass type without requiring every subclass to supply its own type.
Read and write Java 21 regex patterns using character classes, quantifiers, groups, anchors, escapes, and inline flags.
A static { ... } block executes statements during class initialization, not whenever an object is created. Use it to prepare static fields when initialization needs several statements.
A static nested class belongs to an enclosing type, but you do not need an instance of that type to create a nested object.
In Java SE 21, a `switch` expression selects a value. Arrow rules prevent fall-through and let several enum constants share one `case` label.