Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java Bridge Methods: How the Compiler Preserves Type Information

Java bridge methods are synthetic compiler-generated methods that preserve type information after erasure. Learn why they appear with generics and covariant return types, how to inspect them with javap, and how reflection sees them.

~4 min readJava
#Java compiler#Generics#Covariant return types#Reflection
Diagram showing a bridge method delegating from a generic interface method to a specific covariant return method in Java bytecode.

Java bridge methods are synthetic methods that the Java compiler adds to a class file to preserve type information that is lost during generics erasure. They appear in cases such as generic interfaces and covariant return types. This article explains why the compiler generates them, how to inspect them with javap, and how they affect reflection and runtime behavior.

What Bridge Methods Do and Why the Compiler Creates Them

Java generics are implemented through type erasure. At compile time, the compiler removes generic type parameters and inserts casts where necessary. However, erasure alone can break method resolution when a subclass overrides a method with a more specific return type. Consider a generic interface:

java
interface Factory<T> { T create(); }

A concrete implementation might override create to return a specific type:

java
class CarFactory implements Factory<Car> { @Override public Car create() { return new Car(); } }

After erasure, the interface method has the erased descriptor Object create(). The class method returns Car, which is not the same erased signature. To make the override valid at the bytecode level, the compiler generates a bridge method with the erased signature that delegates to the actual implementation:

java
// Synthetic method generated by the compiler public Object create() { return this.create(); // calls the Car-returning method }

This bridge method is marked with the ACC_SYNTHETIC and ACC_BRIDGE flags. It preserves the polymorphic behavior required by the JVM while keeping the source-level covariant return type.

A Minimal Example: Covariant Return Types Without Generics

A bridge method can also be generated for covariant return types even without generics. For instance:

java
class Animal { public Animal reproduce() { return new Animal(); } } class Dog extends Animal { @Override public Dog reproduce() { return new Dog(); } }

Here, Dog.reproduce() returns Dog, a subtype of Animal. The parent method descriptor expects Animal reproduce(), so the compiler adds a bridge method in Dog:

java
// Synthetic bridge method Animal reproduce() { return this.reproduce(); // calls Dog.reproduce() }

This method is not visible in source code, but it exists in the class file. It ensures that calling reproduce() on a Dog through an Animal reference still dispatches to the overriding method.

Inspecting Bridge Methods with javap

Use the javap command-line tool to see bridge methods. Compile the Dog class and run:

bash
javap -c -v Dog.class

The output includes a method with the ACC_BRIDGE and ACC_SYNTHETIC flags. For the Dog example, you will see something like:

text
public Animal reproduce(); descriptor: ()LAnimal; flags: ACC_PUBLIC, ACC_BRIDGE, ACC_SYNTHETIC Code: aload_0 invokevirtual #7 // Method reproduce:()LDog; areturn

The bridge method simply delegates to Dog.reproduce() with no additional logic.

How Bridge Methods Affect Reflection and Method Resolution

Bridge methods are visible to the reflection API. getMethods() returns both the bridge method and the real method, which can be confusing when you are searching for a method by name and signature. For example, Dog.class.getMethods() includes two reproduce methods: one with return type Animal (the bridge) and one with return type Dog (the actual method).

Use Method.isBridge() to filter bridge methods out:

java
Method[] methods = Dog.class.getMethods(); for (Method m : methods) { if (!m.isBridge()) { // process the real method } }

Filtering is important when reflection is used to discover overridden methods or build method tables. Without it, you might invoke the bridge directly. That still works, but it adds an extra call frame and can be confusing in stack traces.

Runtime Cost and Performance Implications

Every call that goes through the erased signature includes an extra method invocation. The JIT compiler often inlines these trivial delegation methods, so the overhead is negligible in normal code. The cost is more noticeable in reflection-heavy code if you accidentally invoke a bridge instead of the real method, adding an extra stack frame in a tight loop.

There is also a small memory cost: each bridge method appears as an extra entry in the class file method table and constant pool. For most applications this is insignificant. The larger concern is correctness: reflection-based proxies and code that matches method signatures must account for bridge methods explicitly.

Common Pitfalls and Debugging Considerations

A frequent pitfall is assuming getDeclaredMethods() returns only the methods you wrote. It also returns synthetic and bridge methods. During debugging, a stack trace may show a bridge method whose name matches your source method but whose return type differs. Knowing that the compiler generates these methods makes the trace easier to interpret.

Generic interfaces with multiple type parameters can trigger multiple bridge methods. For example, a class implementing Comparable<Dog> gets a bridge method compareTo(Object) that delegates to compareTo(Dog). This is required because Comparable is erased to Comparable, whose method signature is compareTo(Object).

Reflection-based frameworks, such as serialization or dependency injection libraries, can behave unexpectedly if they do not filter bridge methods. Always check isBridge() when enumerating methods that should represent source-level declarations.

Edge Cases and When Bridge Methods Are Not Generated

The compiler does not generate a bridge when the overriding method has exactly the same erased signature as the parent method. For example, implementing Runnable and overriding run() needs no bridge because the return type is void and there are no type parameters. Similarly, no bridge is inserted when a subclass overrides a method with the same return type.

Bridge methods also appear when a class implements a generic interface whose type parameter is used in method arguments. Consider:

java
interface Consumer<T> { void accept(T t); } class StringConsumer implements Consumer<String> { @Override public void accept(String s) { } }

After erasure, Consumer has accept(Object), so the compiler generates a bridge method accept(Object) that casts the argument to String and calls accept(String). This cast is essential for type safety at the call site.

Understanding bridge methods helps you interpret bytecode, debug reflection issues, and write robust libraries that handle synthetic members correctly. When you see ACC_BRIDGE in javap output, it indicates a compiler-generated method that keeps erased signatures and source-level type information working together.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.