Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java Marker Interface: Purpose and Practical Use

What a Java marker interface is, how instanceof checks drive its runtime behavior, and when to choose it over annotations.

~4 min readJava
#marker interface#Java type system#annotations#instanceof
Diagram of a Java class implementing an empty marker interface with an instanceof check branching at runtime

A Java marker interface is an interface that declares no methods. Its only purpose is to attach a piece of type information to a class so that code can detect that information at runtime with instanceof. The classic JDK examples are java.io.Serializable, java.lang.Cloneable, and java.util.RandomAccess.

java
public interface Auditable { }

A class implements it without adding any methods:

java
public class Order implements Auditable { private String id; private BigDecimal total; }

The interface itself contributes no behavior. What matters is that Order now carries the Auditable type, and any code can check for it.

How the Runtime Uses the Marker

The JDK's own marker interfaces rely on instanceof checks. For example, Collections.binarySearch checks whether a list implements RandomAccess to decide between indexed and iterator-based search:

java
if (list instanceof RandomAccess) { // indexed binary search } else { // iterator-based binary search }

Similarly, ObjectOutputStream.writeObject checks for Serializable before attempting serialization, and Object.clone refuses to copy an object whose class does not implement Cloneable.

This means a marker interface is not annotation-style metadata. It is a type-level flag that the JVM can test cheaply with a single instanceof instruction. The cost is essentially the same as any other type check.

Common Marker Interfaces in the JDK

InterfacePurposeWhere the check happens
java.io.SerializableMarks classes that may be serializedObjectOutputStream.writeObject
java.lang.CloneableMarks classes that allow Object.clone()Object.clone()
java.util.RandomAccessMarks lists that support fast (generally constant-time) indexed accessCollections.binarySearch and other Collections algorithms
java.rmi.RemoteMarks remote interfaces for RMIRMI runtime

Each one exists because some framework or library code needs to branch on a capability that cannot be expressed through the method signature alone.

Building a Marker Interface for Your Own Code

Suppose you want certain domain events to be written to an audit log. You could define a marker interface and check it in a central dispatch point:

java
public interface Auditable { }
java
public class OrderPlaced implements Auditable { private final String orderId; private final Instant occurredAt; public OrderPlaced(String orderId, Instant occurredAt) { this.orderId = orderId; this.occurredAt = occurredAt; } }

In the event handler:

java
public void handle(Object event) { if (event instanceof Auditable) { auditLog.write(event); } // continue normal processing }

The benefit is that the check is explicit and type-safe. A class either implements the interface or it does not; there is no string matching and no configuration file to keep in sync.

Marker Interface vs. Annotation

Annotations can carry attributes, which makes them more expressive. A marker interface cannot hold data. But a marker interface can be used as a type in method signatures and generic bounds, so the compiler can enforce it.

java
public void persist(Object entity) { if (entity instanceof Persistable) { // ... } }

With an annotation, the same check requires reflection:

java
if (entity.getClass().isAnnotationPresent(Persistable.class)) { // ... }

The reflection call is slower than instanceof and is not checked at compile time. A class can be passed to persist without ever being annotated, and the compiler will not complain. With a marker interface, you can constrain the method signature:

java
public void persist(Persistable entity) { // ... }

That is the strongest argument for a marker interface: it turns a runtime convention into a compile-time contract.

Where Marker Interfaces Break Down

A marker interface cannot be applied to a class you do not control. If a third-party class would logically be Auditable but does not implement the interface, you cannot add it without wrapping or subclassing. An annotation cannot be added to the already-compiled class either; both require source access or bytecode manipulation.

Marker interfaces also do not scale well when the marker needs to carry configuration. If you later need to record the audit retention period, you must either add a method to the interface, which breaks every implementer, or switch to an annotation with an attribute.

Another limitation is that instanceof checks scattered through the codebase can become hard to trace. If many unrelated classes implement the same marker, the check sites become the only documentation of what the marker means.

Choosing Between Marker Interface and Annotation

Use a marker interface when:

  • The marker must be enforced at compile time through method signatures.
  • The check happens frequently and the cost of reflection matters.
  • The marker is part of a public API where type safety is valuable.

Use an annotation when:

  • The marker needs attributes.
  • Existing processing code is already built around reflection and annotations.
  • The marker is consumed by a framework that already uses reflection.

A practical hybrid exists: define a marker interface for the type contract and an annotation for configuration. The interface drives compile-time checks, and the annotation carries the details.

Compatibility and Maintainability Considerations

Adding a marker interface to an existing class is a binary-compatible change. Existing code that references the class keeps working, and new code can start checking for the marker. Removing a marker interface, however, is breaking: any code that checks instanceof will change behavior, and any method signature that references the interface will fail to compile.

Because marker interfaces are part of the type hierarchy, they become part of the public contract. Once clients rely on instanceof Auditable, you cannot remove it without a major release. Treat a marker interface as a commitment, not a temporary flag.

The JDK has tended to prefer annotations for new marker-style metadata, but the existing marker interfaces remain, and understanding how they behave is still necessary for reading and maintaining code that uses serialization, cloning, or collection algorithms.

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.