If a Marker Interface does not have any methods, how does it work?

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?


Solution 1:

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.

Solution 2:

The only useful thing you can do with it is

if (instance instanceof MyMarkerInterface) {
   ...
}