What is the use of marker interfaces in Java?

2019-01-02 20:24发布

When there is nothing to implement in the marker interfaces like Serializable . . what is the use of implementing it?

9条回答
公子世无双
2楼-- · 2019-01-02 20:53

Looking carefully on marker interface in Java e.g. Serializable, Clonnable and Remote it looks they are used to indicate something to compiler or JVM. So if JVM sees a Class is Serializable it done some special operation on it, similar way if JVM sees one Class is implement Clonnable it performs some operation to support cloning. Same is true for RMI and Remote interface. So in short Marker interface indicate, signal or a command to Compiler or JVM.

Read more: http://javarevisited.blogspot.com/2012/01/what-is-marker-interfaces-in-java-and.html#ixzz2v6fIh1rw

查看更多
临风纵饮
3楼-- · 2019-01-02 20:54

The main purpose is to tell the compiler that treat differently for the object of the class which implemented marker interface.

查看更多
皆成旧梦
4楼-- · 2019-01-02 21:04

Joshua Bloch: Effective Java 2nd Edition, p 179

Item 37: Use marker interfaces to define types

... You may hear it said that marker annotations (Item 35) make marker interfaces obsolete. This assertion is incorrect. Marker interfaces have two advantages over marker annotations. First and foremost, marker interfaces define a type that is implemented by instances of the marked class; marker annotations do not. The existence of this type allows you to catch errors at compile time that you couldn’t catch until runtime if you used a marker annotation....

Personally I think I'll bow to Joshua's superior knowledge on this subject.

查看更多
低头抚发
5楼-- · 2019-01-02 21:06

In earlier versions of Java, Marker Interfaces were the only way to declare metadata about a class. For example, the Serializable Marker Interface lets the author of a class say that their class will behave correctly when serialized and deserialized.

In modern Java, marker interfaces have no place. They can be completely replaced by Annotations, which allow for a very flexible metadata capability. If you have information about a class, and that information never changes, then annotations are a very useful way to represent it.

查看更多
无色无味的生活
6楼-- · 2019-01-02 21:06

You are right in reasoning that an empty interface does not affect the "standard" execution of the program which is based on inspection/mutation of fields and dispatching of methods.

However, marker interface are useful when used in conjunction with reflection: A library/method inspects (via reflection) an object and works differently if its class impplements the marker interface. As of Java5 there's very little need for marker interfaces - the same "marking" facility can be achieved via Java annotations - which (again) most of their effect will be achieved via reflection-based code.

查看更多
情到深处是孤独
7楼-- · 2019-01-02 21:08

Such marker interfaces are useful in the case other code takes decisions depending on whether an object implements some marker interface.

In the case of Serializable, reflection will be used to serialize the fields of the objects.

Now annotations are preferred as they don't propagate to sub-classes.

See Marker interface pattern.

查看更多
登录 后发表回答