1

I know something about 'marker interface', a marker interface doesn't have any members. For example: Serialiazable, Cloneable etc.

I found in googling that the marker interface is for sending some instructions to JVM? I want to know what are those instructions and how I could understand? Please help me with a real world example.

user207421
  • 305,947
  • 44
  • 307
  • 483

3 Answers3

2

A marker interface is a means of marking that a class is or does some things and even those things are not expressed as actual methods. For example Serialiazable,Cloneable etc. And since annotations are introduced they are almost always preferred though they cannot replace completely Marker Interface, as suggested by Joshua Bloch

... 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....

In case of Serializable, a class implements this interface to indicate that its non-transient data members can be written to an ObjectOutputStream. Also a serialized object needs to retain compatibility across systems and it is an explicit design decision and hence requires the use of the marker interface, to identify such candidates.

Similarly implementing Cloneable tells JVM that this class implements Cloneable and hence JVM will have to copy it bit-wise.

akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
  • Last sentence is very sloppily expressed. 'Implementing `Cloneable` tells JVM that this class implements `Cloneable` 'is a mere tautology. – user207421 May 02 '15 at 08:22
2

found in googling that the marker interface is for sending some instructions to JVM?

No. They are used as a marker to other Java code.

I want to know what are those instructions

It is nothing more exotic that an instanceof test.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

A marker interface doesn't have an implementation.

However, a marker interface will typically influence the behaviour some other classes.

I found in googling that the marker interface is for sending some instructions to JVM?

That is incorrect.

What it is doing is providing information to either the JVM or to classes either provided by or running on the JVM. The information is NOT in the form of "instructions". Rather it is a label at the class level.

For example, when you declare a class as Serializable, you are passing information to the ObjectInputStream and ObjectOutputStream that instances of that class can be serialized. You are passing that information in the form of the class itself; i.e. ObjectInputStream and ObjectOutputStream can use instanceof (or equivalent) to test if an object is serializable.

Please help me with a real world example.

Serializable and Cloneable are the well-known examples in the Java class libraries.

There are other interfaces which have an "marker-interface-like" aspect. For example Closeable and Autocloseable both have a single close() method that does the same thing. The difference is that AutoCloseable is treated as a marker by a "try with resources" statement.

Finally, it is worth nothing that marker interfaces are frowned on by OO design purists. You can achieve the same end using Java annotations.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216