Erasure and Bridge Methods in Java
See how Java bridge methods preserve generic overrides after type erasure, inspect representative javap output, and distinguish bridges from overloads.
See how Java bridge methods preserve generic overrides after type erasure, inspect representative javap output, and distinguish bridges from overloads.
Erasure and Bridge Methods in Java
Bridge methods are synthetic methods that the compiler adds when an override’s bytecode descriptor differs from the inherited erased descriptor. The bridge keeps calls through the generic supertype dispatching to the subtype implementation.
Introduction
Type erasure means generic type arguments do not affect JVM method descriptors. An unbounded type variable typically erases to Object, while a bounded variable erases to its leftmost bound. Class files can still retain generic signature metadata for tools and reflection. The compiler adds a bridge when needed so a call using the erased descriptor still reaches the typed override.
Bridge methods do not appear in Java source, but tools such as javap and reflection can show them. For example, a subclass can declare String get() while the generic superclass declares T get(). The class file then contains both the typed method and a synthetic Object get() bridge.
This guide shows a generic override, its bridge in bytecode, and how that compiler-generated method differs from a source-level overload.
The Core Problem
Consider a generic supertype and a concrete subtype:
public class Node<T> {
public T get() { return null; }
public void set(T value) { }
}
In Node.class, the unbounded T is erased to Object, so the method descriptors are get(): Object and set(Object): void.
Now if you extend this with a concrete type:
public class StringNode extends Node<String> {
@Override
public String get() { return "value"; }
@Override
public void set(String value) { }
}
The source method remains String get() in the class file, while the inherited generic method has the erased descriptor get(): Object. The compiler emits an Object get() bridge that forwards to String get(). It also emits a set(Object) bridge that casts its argument to String before calling set(String).
What Is a Bridge Method
The bridge is part of the compiled class, not another method to write in Java source. A compiler may need one when erasure changes a parameter descriptor, as with set(T) becoming set(Object), or when a covariant return needs the inherited return descriptor. The bridge forwards to the source method and may cast arguments to the concrete type.
An overload is different: a programmer writes both source methods with different parameter lists, such as format(String) and format(String, int). The compiler does not invent the second method. Java also does not allow two source methods that differ only by return type, so you cannot manually add both String get() and Object get() to the same class.
class Formatter {
String format(String text) { return text; }
String format(String text, int maxLength) {
return text.substring(0, Math.min(text.length(), maxLength));
}
}
Both format methods are written in source and overload by parameter count. Neither is a compiler-generated bridge.
Code Example: Seeing Bridge Methods
The Node<T> and StringNode source above can be compiled and inspected with OpenJDK javac 21.0.13:
javac -g -d out Node.java StringNode.java
javap -classpath out -c -v StringNode
Representative output from javap -c -v StringNode on that JDK:
public void set(java.lang.Object);
descriptor: (Ljava/lang/Object;)V
flags: (0x1041) ACC_PUBLIC, ACC_BRIDGE, ACC_SYNTHETIC
Code:
0: aload_0
1: aload_1
2: checkcast #9 // class java/lang/String
5: invokevirtual #11 // Method set:(Ljava/lang/String;)V
8: return
public java.lang.Object get();
descriptor: ()Ljava/lang/Object;
flags: (0x1041) ACC_PUBLIC, ACC_BRIDGE, ACC_SYNTHETIC
Code:
0: aload_0
1: invokevirtual #17 // Method get:()Ljava/lang/String;
4: areturn
The constant-pool indexes can vary by source details and compiler build. The important signs are the erased descriptors, ACC_BRIDGE, ACC_SYNTHETIC, and the forwarding instructions. The set(Object) bridge casts before calling set(String); get() forwards and returns the typed result.
Mermaid Diagram: Bridge Method Delegation
classDiagram
class Node~T~ {
+get(): T
+set(T): void
}
class StringNode {
+get(): String
+set(String): void
}
Node~T~ <|-- StringNode
StringNode : compiler adds get(): Object bridge
StringNode : compiler adds set(Object): void bridge
Code Example: Covariant Return Types with Bridge Methods
class Numeric {
public Number value() { return 0; }
}
class IntegerBox extends Numeric {
@Override
public Integer value() { return Integer.valueOf(42); }
// Bridge generated:
// public Number value() { return this.value(); }
}
Java also emits a bridge for a covariant return override. IntegerBox.value() returns Integer, while the bridge retains the inherited Number value() descriptor.
Failure Scenarios
1. Raw Type Call Reaches a Bridge Cast
A raw reference bypasses generic type checks at compile time. The generated bridge still checks the runtime argument type, so a mismatched value fails with ClassCastException:
public class RawTypeCall {
public static void main(String[] args) {
Node raw = new StringNode();
raw.set(42); // Compiles with raw-type and unchecked warnings; throws ClassCastException.
}
}
The set(Object) bridge casts its argument to String before calling set(String). Use a parameterized reference such as Node<String> so the compiler rejects incompatible values before runtime.
2. Collision After Erasure
public class ClashingMethods<T> {
public void set(T value) { }
public void set(Object value) { } // Does not compile: same erasure as set(T).
}
The type variable T erases to Object, so both source declarations have the descriptor set(Object). This overload is invalid; the compiler reports a name clash instead of generating a bridge.
3. Visibility and NoSuchMethodError
A Java override cannot reduce the visibility of an accessible superclass method. If a public generic method is overridden with package-private visibility, the source does not compile, and a compiler-generated bridge keeps the access needed for that override. OpenJDK javac 21.0.13 also rejects @Override Object get() in StringNode extends Node<String> with return type Object is not compatible with String.
A NoSuchMethodError instead points to a runtime linkage mismatch, such as code compiled against a method descriptor that is absent from the class loaded at runtime. Check the deployed class files and descriptors with javap when diagnosing that error.
Bridge Methods vs. Overloads
| Feature | Bridge method | Programmer-authored overload |
|---|---|---|
| Where it comes from | Added by the compiler to a class file | Declared in Java source |
| Signature | Matches an erased inherited descriptor | Uses a distinct parameter list |
| Purpose | Preserves dispatch to a generic override | Adds a separate call selected by its arguments |
| Reflection | Method.isBridge() and isSynthetic() are true |
Not marked as a bridge or synthetic |
Observability Checklist
- Use
javap -c -vto inspect synthetic bridge methods in compiled bytecode - In stack traces, bridge methods can appear — know how to filter them
- Static analysis tools sometimes flag bridge methods as redundant — suppress only if certain the behavior is correct
- Ensure test coverage exercises generic override paths to catch incorrect bridge delegation
- For framework authors, test that subclassing with generic overrides works across package boundaries
Reflection Notes
Reflection can return both a source method and its generated bridge. Use Method.isBridge() to identify bridges; Method.isSynthetic() also reports other compiler-generated methods. Frameworks that enumerate methods should decide whether to keep or filter bridge methods based on how they invoke overrides.
Pitfalls
-
Confusing bridge for real method: Reflection can show both
Object get()andString get(). CheckMethod.isBridge()to identify the compiler-generated method. -
equals() / hashCode() / toString(): These methods are not subject to erasure bridge generation — they are already declared in
Objectwith the exact signatures used at runtime. -
Generic interface implementation: The same bridge mechanism applies when a generic interface is implemented. Implementing
Comparator<String>withint compare(String a, String b)generates a bridgeint compare(Object a, Object b). -
Clash detection: If two methods would have the same erasure signature, the compiler emits “name clash” errors — this catches cases where your overloaded methods become ambiguous after erasure.
Quick Recap Checklist
- Bridge methods are compiler-generated and marked
ACC_BRIDGEandACC_SYNTHETICin the class file. - An erased descriptor can forward to the typed override, with an argument cast when needed.
- Use
javap -c -vorMethod.isBridge()to identify a bridge. - Distinguish bridges from overloads declared in source with different parameter lists.
- Raw types can bypass generic checks and trigger a bridge cast failure at runtime.
Interview Questions
Further Reading
- Type Erasure — how generics are erased at compile time
- Generic Classes — defining classes with type parameters
- Generic Methods — methods with type parameters
- Wildcards —
? extends Tand? super T - Type Bounds — upper and lower bounds on type parameters
- Oracle: Bridge Methods - Official documentation on compiler-generated bridge methods
- OpenJDK: Method reflection API - Source for bridge and synthetic method metadata
Conclusion
Type erasure gives Node<T>.get() the class-file descriptor get(): Object. StringNode declares get(): String, so the compiler adds an Object get() bridge that forwards to the typed method. Its set(Object) bridge casts to String before calling set(String).
These are compiler-generated methods, not overloads written in Java source. An @Override Object get() declaration in StringNode extends Node<String> is rejected by javac; it cannot replace the generated bridge. Raw types can still pass a value that fails the bridge’s runtime cast.
Use javap -c -v to inspect the descriptors and flags, or Method.isBridge() when examining methods through reflection. For the broader runtime behavior of generics, see Type Erasure in Java Generics.
Category
Related Posts
Abstract Classes in Java: Interfaces and Composition
Compare Java interfaces, abstract classes, and composition, then see when shared state, default methods, or sealed types change the design choice.
Arithmetic Operators in Java: Division, Overflow, Promotion
Compare Java arithmetic with runnable examples of division, negative remainders, overflow, type promotion, and BigDecimal for decimal money.
Java Array Basics: Length, Bounds, Copies, and Resizing
Learn Java arrays through runtime-sized allocation, bounds checks, aliasing, copying, and the fixed length that distinguishes them from ArrayList.