I am aware of what marker interface is and when we need to use it. One question is still not clear to me. If a marker interface does not have any method or body, how does it work at runtime?
5 Answers
A marker interface doesn't "work" as such. As the name suggests, it just marks a class as being of a particular type. Some other code has to check for the existence of the marker and do something based on that information.
These days annotations often perform the same role that marker interfaces did previously.

- 53,737
- 19
- 129
- 165
-
Hi Dan, just correct me if my understanding is wrong. As the name says "marker", does it means that as soon as any class implements marker interface it is implied that at runtime JVM will use some mechanism to identify this class as marked to implement the interface like cloneable. – Kamal Dec 27 '10 at 18:28
-
@Dan,Since you told `"Some other code has to check for the existence of the marker and do something based on that information."`.,Could you provide a example/scenario for the same, – Deepak Dec 27 '10 at 18:47
-
1@Kamal No, a marker interface has no special meaning to the JVM, and by itself results in no additional functionality. In the case of Cloneable, it just tells other code that the developer of this class warrants that instances are properly cloneable. Cloneable is arguably a good example of a marker interface being misused. The interface should really enforce the provision of a public clone method. – Dan Dyer Dec 27 '10 at 18:48
-
@Deepak I can't think of a good example right now that I wouldn't write in some other that didn't involve a marker interface. Marker interfaces aren't really that useful (there is no more to them than mklhmnn demonstrates). Discussion of `Cloneable` or `Serializable` is misleading because the actual cloning and serialisation mechanisms are not really dependent on the fact that marker interfaces are used to indicate which classes may be cloned or serialised. – Dan Dyer Dec 27 '10 at 19:03
-
Rolled back the edit on this because it completely changed the answer and wasn't very clear in my opinion. I don't mind edits in general but if you want a completely different answer, please add your own rather than changing mine as my name remains listed against it. – Dan Dyer Jan 08 '13 at 11:11
The only useful thing you can do with it is
if (instance instanceof MyMarkerInterface) {
...
}

- 28,248
- 23
- 84
- 121
Marker interfaces can be replaced with annotations in many places, however a marker interfaces can still be used for
The compile time checks. You can have a method which must take an object of a class with a given marker interface(s) e.g.
public void myMethod(MyMarkerInterface MMI);
You cannot have this compile time check using an annotation alone.
BTW: You can have two interfaces using generics, but good examples are rare.
- Support frameworks which depend on interface(s) to identify a component type. like OSGi.
EDIT: I use this for a Listener marker interface. A listener has methods methods marked with annotations but the methods can have any name or type. It adds a compiler time check to what would otherwise be a purely runtime linking.
public Component implements Listener {
@ListenerCallback
public void onEventOne(EventOne... eventOneBatch) { }
@ListenerCallback
public void onEventTwo(EventTwo eventTwo) { }
}

- 1,542
- 4
- 24
- 37

- 525,659
- 79
- 751
- 1,130
-
3Your `myMethod` is more or less useless, because what could be done with a *marker* interface which does not have more methods than `Object`? – Mot Dec 27 '10 at 19:58
-
@MikeL. Look at the documentation of `java.lang.Cloneable` and `java.util.RandomAccess` for "good" examples (_good_ as in _I'd done it this way before annotations have been introduced_). – Feuermurmel May 31 '13 at 12:43
-
@Feuermurmel I wouldn't count Cloneable as a good example of a marker interface as it's not clear why it doesn't have a `public Object clone()` method. – Peter Lawrey May 31 '13 at 15:18
-
1@MikeL. I also don't like how cloning is implemented in Java, but if `Cloneable` declared such a method, how could a class benefit from the automatic cloning if a shallow copy was requested? I change my vote from `Cloneable` to `java.rmi.Remote`. – Feuermurmel May 31 '13 at 19:22
Marker interface in Java is interfaces with no field or methods or in simple word empty interface in java is called marker interface. e.g. serializable, Clonnable and Remote Interface. They are used to indicate signal or command to the compiler Or JVM. It can also be used to classify code. You can also write your own marker interface and use them to logically divide your code. Also, you can write any pre-processing operation on those class.

- 750
- 1
- 9
- 22
A marker interface tells JVM that the class being marked by marker interface to add functionality of a marker interface . Like implementing Cloneable tells JVM that this class implements Cloneable and hence JVM will have to copy it bit-wise.

- 1
- 1