Java Marker Interface

Last modified on July 7th, 2015 by Joe.

Marker interface is used as a tag to inform a message to the Java compiler so that it can add special behaviour to the class implementing it. Java marker interface has no members in it.

Lets take the java.io.Serializable marker interface. It does not have any members defined it it. When a Java class is to be serialized, you should intimate the Java compiler in some way that there is a possibility of serializing this java class. In this scenario, marker interfaces are used. The java class which may be serialized has to implement the java.io.Serializable marker interface. In such way, we are intimating the java compiler.

From java 1.5, the need for marker interface is eliminated by the introduction of the java annotation feature. So, it is wise to use java annotations than the marker interface. It has more feature and advantages than the java marker interface.

Is Runnable a marker interface?

I could not find any reference or definition to marker interface from Java specification or API. It looks like, ‘Marker Interface’ is a term coined by authors. This is one popular question asked in Java interviews.

“Runnable is not a marker interface”, you might say ‘run’ is used to start a method and this is a special instruction to the JVM.  When you run a java class the java interpreter calls the ‘main’ method, because of this would you say every java class as “marker class”?

Marker Interface

“An interface is called a marker interface when it is provided as a handle by Java interpreter to mark a class so that it can provide special behaviour to it at runtime and they do not have any method declarations”.

I don’t think, in future there will be any new marker interfaces added.

We cannot create marker interfaces, as you cannot instruct JVM to add special behavior to all classes implementing (directly) that special interface.

Java Marker Interface Examples

Comments on "Java Marker Interface"

  1. […] for copy or to the least it should invoke the super.clone(). Also you have to implement Cloneable marker interface or else you will get CloneNotSupportedException. When you invoke the super.clone() then you are […]

  2. Stefan says:

    Dear Joe
    I have been reading books and blogs related to this topic of marker interface but I find everyone of them convoluted and unclear on the exact purpose. several questions and concern arise based on the explanations. For example consider marker interface serializable. How does the compiler differentiate between the two classes since their is no members defined in serializable interface.

  3. Harris says:

    Good explanation Joe !!
    @Stefan : In case of a marker interface i guess there is a type check like :
    if(myRef instanceOf Serializable) {
    // do serialiazation stuff

    }
    with which the compiler knows about the marker interfaces (and their special meaning). As of java 1.5 marker interfaces are obsolete and annotations should be used otherwise.
    Correct me if i am wrong.
    Good luck :)

  4. Alps says:

    Gud xplanation but have some queries.
    Wats the use of a Marker Interface in Java. When shall we can use user defined interface as marker interface.

  5. Allen says:

    how the Java compiler differentiate normal classes and Java classes implementing these marker interfaces even though the marker interface dont have any members

  6. Parker says:

    Nice Explanation……
    one query- Can you Provide an Example for marker interface ….??

  7. Mandava says:

    Hi frnds give me a clear clarification regarding Java marker interfaces..
    why use them,wat is the need..?

  8. Jana says:

    Marker interface does n’t have any method declaration, It tells to the compiler “This class for that purpose”….
    ie. Cloneable, serializable, eventListener

  9. Ashish says:

    private class bean implements Serializable{
    int id;
    String name;
    public int getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }

    }

  10. santosh says:

    I think Java compiler won’t recognize the interface is a marker interface or not,only Java JVM will recognize it.If we are using serialization concept to save state of a object without implementing that class to the Serializable interface then we will get a runtime exception saying NotSerializableException.So from my point of view compiler won’t know anything about whether a interface is marker or not,only Java JVM is internally designed in such a way to provide some new ability to the class which implementing the marker interface.
    Some examples of Java marker interfaces are :-
    1) Serializable
    2) Cloneable
    3) RandomAccess
    4) SingleThreadModel
    5) EventListner

  11. Carter says:

    please,i would like to know in a clarity about the “Marker Interface” with example.

  12. Kevin says:

    I think the comment from :santosh (on October 5th, 2011 11:20 am), is correct.It should be providing a msg to Java VM rather than compiler.

  13. Ankur says:

    Can you plz provide an example how we can use annotations in place of marker interfaces.

  14. RAJ says:

    sir
    you are told marker interfaces are not contain methods then externalizable class is containing the own methods but it is a extends of the serilazable . then im saying marker interfaces are may be contaning the methods or may not be containing methods

    example :java.io.externalizable

  15. Evans says:

    Hi Joe,

    I have been analyzing marker interface for more than one month .I found that they are two groups of people wrote solution

    Group I : Marker interface does not contain any methods.It is just for giving information to compiler that this class will do seralization/cloneable operation.

    Group II : Runnable is a marker interface.It has method of run().So,marker interface need not be empty methods.

    I found that there is no keyword used as like ‘marker’ in java api in Serializable/Cloneable as well as Runnable .

    So, Could you give your suggestion on this ?

  16. Brahmi says:

    Is Runnable is a marker interface or not?

  17. Geetanjali says:

    I want Purpose of Java marker interface. I know What is marker Interface Can u tell me? ………

  18. kavitha says:

    Is there any other way to make Serializable other than implementing Serializable Interface.I had interview today morning they asked this question.Can any body pls help me.

  19. Joe says:

    @Kavitha,

    you can use the Externalizable Interface. It has two methods, writeExternal(..) and readExternal(..). You should override these two methods and provide your implementation (protocol) for serialization.

  20. Anantha Prakash says:

    @Geetanjali,

    The main purpose to have marker interfaces is to create special types in those cases where the types themselves have no behavior particular to them.

    This will give birth to another question like
    If there is no behavior then why to have an interface?

    Because the implementor of the class might only need to flag that it belongs to that particular type and everything else is handled/done by some other unit – either internal to Java (as in the case of Java supplied standard marker interfaces) or an app specific external unit.

  21. Baker says:

    as we know that marker interface doesn’t have any method. but the question is why to use marker interface like cloneable or Serializable type of interface. and how it works?? actually for an example using clone() method is not secure operation for jvm. so what marker interface does, it will give one mark or u can say one permission to jvm that plz grant this operation as a secure operation. and jvm permit this operation. so marker interface is used for marking the permission to any method.

  22. kavitha says:

    Thank you so much Joe for explaination of my query,It is really nice site for Java people.Thanks lot for your effort.

  23. suraj says:

    As far as I have researched,Marker Interface means an interface which can provide special behavior to its child object.
    The presence of Method is not really necessary,But the behavior is Important.

    Some people say interface w.o method is called marker interface.
    My question back to them is,if u define an interface w.o any method,will JVM treat it as marker.Certainly not..

  24. Joe says:

    @Suraj,

    “Runnable is not a marker inteface”.

    I have added my comment in detail at the end of the article.

    Thanks for stopping by and adding the comment.

  25. Smith says:

    Good job done, the best Java tutorial for marker interface in net.

  26. Preethi says:

    joeeee, u are special, thanks….

  27. Sudeer Verma says:

    nice explanation of Java marker interface

  28. Nelson says:

    thanks for your stuff..

  29. Dhanesh says:

    Great captions for this topic. Crisp and fruitful stuff.

    Thanks.

  30. Perez says:

    Good one Joe

  31. krishna says:

    Hi , pls tell me

    what is the best practices of Java collection frame work? can you write a Java tutorial on this?

  32. Anonymous says:

    how Java jvm identify the class implements MarkerInterface or not?i mean based on serialversion uid or instanceof operator.

  33. Jai says:

    Sir please explain anonymous inner class.

  34. neha says:

    please define java for me

  35. naveen says:

    but camparable interface is marker interface and has compareto() method how?

  36. arivoli says:

    Good explanation Joe !!

  37. Davis says:

    excellent material guidance

  38. Praveen Rohal says:

    excellent work Joe… the best Java tutorial … thanks.

  39. Deepthi says:

    There is absolutely nothing special about a marker interface — it’s just an interface with no methods. The only thing that makes them matter is that there’s code somewhere that checks “instanceof” against them; for example, the java.lang.Object version of clone() checks “this instanceof Cloneable” before it will actually do anything.

  40. venkata says:

    Hi,

    How Java JVM understand serialization?
    I mean how this is implemented in Java JVM, As this is market interface no method will there and its just sign to JVM saying that to serialize object.

    Could you please tell me how serialization is done in Java JVM and how it understands by sign or keyword.

    thanks,
    Venkata

  41. satyanand singh says:

    excellent answer—-well defined—-way of documentation from begin 2 end was serial and it was damn xcelnt

  42. Lee says:

    Good explanation.

  43. shilly says:

    what is the use of Java marker interface?

  44. sri says:

    How to write our marker interface? what kind of changes we need to do sothat the compiler will recognize it as marker interface?

  45. anji says:

    How exactly we can find it is a Java marker interface(except behavior thing ). How can identify Java JVM. All interfaces are Marker interfaces, which are not have behaviors.

  46. Brown says:

    Can you give some more real time examples in Java for marker interface

  47. kamalakkannan says:

    purpose of Java markerInterface?
    purpose of null Interface?

  48. atul says:

    Awesome sir!! I a beginner in Java and I am learning using your Java tutorials. Thanks a lot sir.

  49. Subbhu Reddy says:

    this marker interface process is evaluated by Java JVM at runtime. thanks for the tutorial.

  50. Fareed says:

    Good Explanation for market interface .

    Thanks Q Very Much

  51. Fareed says:

    Opps its marker interface not a market interface

  52. Paras Jain says:

    Your explanation and language is very good, easy to understand

    the best Java tutorials collection..
    Keep It Up…

  53. Paras Jain says:

    Can we Override a constructor in Java?

    If Yes then how ?

    If no then why ?

    I am waiting for your answer…

  54. Fred says:

    How can we write our Custom Java marker inteface

  55. Mamta says:

    Consider 2 students X,Y in a class. X stood first in class, Even-though both are in same class, having similar properties and behavior. one can differ the both based on the stuff. There is no use of marking a person as smart. But it adds a special property to the person. Like marker Interface, JVM creates some interfaces that make a difference to classes. It just do nothing but adds a special behaviour.

  56. Siddesh Sahu says:

    if you extends any marker interface than your interface also become marker interface it will acquire all the properties of marker interface because some where in jvm it uses instanceof property to check. but we are still not able to change the behavior of jre to create directly our own marker interface without extending any existing marker interface

  57. Mamta says:

    So, If a extend a marker interface then my interface is also a marker interface.
    Can I declare methods in that?
    (Marker interface should not have a single method)

  58. anjaneyelu says:

    yes !! I will agree with this statememt.ie A Marker interface may or may not contain methods for ex. The externalizable interface contain readInternal() and writeExternal() methods

  59. Anjaneyelu says:

    As my knowledge Concern a marker Interface may contain methods for ex. Java.Lang.Runnable, Java.Lang. Externalizable and some more…..

  60. Anjaneyelu says:

    As me Concern Runnable is a marker Interface why because if our java class is not implements to the Runnable Interface then the object will act as normal object. that means it contains all state and behaviour of the class. otherwise if our class is implements Runnable Interface then the Implemented class object will get special behaviour given by the JVM. That means it will act as a Thresd unlike Process

  61. Anjaneyelu says:

    As me concern an Interface called as a Marker Interface if and only if, the implemented class object will get special identity and behaviour at the time of runtime rather than normal behaviour.

    if our class implements marker Interface , the implemented class object will get Special Identity and behaviour given by the JVM. For Ex.
    A java class implemets Runnable Interface , then the Object will acts as a Thread
    A Java class that implements Serializable, then the implemented class object will get such a behaviour it can tranfer across the Network
    like this many more like
    Java.lang. Clonable
    Javax.servlet.SingleThreadModel

  62. manoj says:

    Hi joe,
    We can create marker interface.
    @Local
    public interface ChartFacadeLocal extends IChartFacade{

    }

  63. Johnson George says:

    Nice Joie..

  64. Nitin says:

    Ram, You are right in saying that compiler cant differentiate. Infact in the terms of marker interface, compiler has no role to play. Its only JVM who act based on the indication of marker interface NOT compiler. For example cloning is verified at runtime whether class implementing Cloneable or not. Same case with Serializable.

  65. Nitin says:

    Its not correct. compiler does not check or complain related to marker interface mandate.

  66. sree says:

    hi,
    actually the clone method has the logic related to marker interface,
    mean at the time of calling the clone() on the object it checks weather the object instantiated from cloneble interface or not ,if yes it creates the copy of that obj other wise it throws Exception.it checks like this.
    if(o instanceOf clonableObgect )
    {cloning}
    else
    {exception}
    according to my study

  67. abhishek says:

    thanks

  68. Ram says:

    Joe
    I have occasionally visited this blog in past few years. I was teaching a new developer about marker interface and was doing preparation. When I read your blog I noticed below comments
    From java 1.5 the need for marker interface is eliminated by the introduction of annotation feature. This is a common misunderstanding among lot of people. I happened to read Josh’s book Effective Java and in item 37 he clearly states the advantages of both and when to use each one of them. One difference I want to bring is
    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 run-time if you used a marker annotation.
    Hope I was able to add some value

  69. sita says:

    Marker interface’s definition is given in all books.This term came from books only.And moreover if you wanna make your understanding better of marker interface.You may refer Effective java by Joshua Bloch or you may refer stack overflow.Because this tutorial is correct in a way but it’s incomplete

  70. DurgaGanesh says:

    I am totally get confused. here is my queries are…..

    Can we create User-defined maker interface. ?

  71. DurgaGanesh says:

    I am totally get confused. here is my queries are…..

    Can we create User-defined maker interface. ?
    Does maker interface contains any methods or not?
    Is this true to state as “Runnable interface is marker interface” ?

    Thanks.

  72. sita says:

    Runnable interface is not a marker interface because Runnable has a method run().Marker interfaces don’t have any methods.Marker interfaces are used to tell JVM to make class implementing marker interface to behave as per that interface.For Example,cloneable interface is a marker interface.Class implementing cloneable will tell JVM that this class can be cloned.

  73. Madhusudana says:

    Hi Joe Can U tell me the Process Of Creating My Own Marker Interface With An Example….

  74. naveenkumar says:

    please help me..
    I want my own interface as marker interface. i will implements that interface to my class, than my class as active like seriazable object…

  75. Clark Matthews says:

    Mr Joe can you explain me Is Runnable a Marker interface if it is Why

  76. Sandeep shukla says:

    Dear Joe,

    I feel I owe to you. Thanks man for such nice and simple explanation I loved it….cheers !!

  77. prasad says:

    if no methods in interface then it is marker inter face but it is wrong we can use seralizable interface to use an interface
    for example java.lang.EventListener
    public void seralizable
    {
    void show()
    {
    system.out.println(“seralizable”);
    }
    }

  78. Andy says:

    All your Java tutorials are excellent. Are you providing any video tutorials?

  79. rashmi ranjan jena says:

    what is markerinter face………

  80. Prasanth Pillai says:

    Hi Kavitha,

    You may use deep cloning ( Deep Copy) which gives the same purpose of Serialization.

    Regards,
    Prasanth Pillai

  81. […] you want to serialize an object, that respective class should implement the marker interface serializable. It just informs the compiler that this java class can be serialized. You can tag […]

  82. om kumar says:

    i think marker interface is implemented by the jvm and jvm is responsible for the object to be serialize at the runtime .in compile time jvm has no idia about the class but when its compiled then every thing is cared by the JVM

  83. Scott says:

    Thank u joe,.
    The best tutorial for marker interface?

  84. Jason Wright says:

    Does it make any vital difference, whether it is sending message/information to the compiler or Java JVM? This topic is all about Marker Interface and Joe explained it very well :)

  85. Divya says:

    Very good article..
    Thanq joe

  86. muthukrishnan says:

    How compiler identify this is marker interface rather than normal interface?

  87. Manibharathi says:

    Hi Joe,
    Marker Interface is verfied by compile time or Runtime?

  88. Mitul says:

    Marker interface has some special privilleges in JVM.To enforce some licence mechnisam to

    secured process.Not possible to create user defined marker interface. They areregisterd in

    JVM

  89. Ken says:

    Marker Interfaces are give about the implementing classes.

    Interface in java defines a specific behavior while class can define both behavior and state. So Interface is pure behavior and class represent state and behavior. Interfaces are used in Java to specify the behavior of classes that implement those interfaces.. interfaces in Java that have no behavior are known as marker interfaces. They have no method defined in them but are absolutely empty.

    examples of marker interfaces

    Java.lang.Cloneable
    If you want to added cloneable feature in a class ,that class needs to implement Cloneable interface.

    java.io.Serializable
    Serialization is nothing but s saving the state of an object to persistent storage as byte stream. Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. The serialization interface has no methods or fields .

    java.util.EventListener
    A tagging interface that all event listener interfaces must extend.

  90. Ben says:

    Thanks for this nice tutorial. Can I create my own marker interface?

  91. ramesh says:

    Hi guys,

    Anybody tell me,

    How to create own MARKER INTERFACE in Java

    Thanks,
    Ramesh.

  92. Lewis says:

    Dear joe ,

    suppose i create marker Interface name “MyInterface”
    in this case what is the role of JVM for treating the class that implements MyInterface

  93. Tarun Soni says:

    hello sir i have one question how to make a custom marker interface . and how to show to jvm its marker interfCE AND ADD some special behavior to the class implementing

  94. Roushan Singh says:

    It’s very simple just define an interface without having any property or behaviour.
    There are many advantages of Marker Interface in which one we can see easly that we can create the object or instantiate the Interface, which is simply not possible in Java.

  95. Hill says:

    Hi, explanation is good. Marker interface is not a term coined by books and writers. In the Java API source file of RandomAccess.java it is written that it is a marker interface.

  96. Philips says:

    This the best tutorial for marker interface. Thanks Joe for all your Java tutorials. Good work.

Comments are closed for "Java Marker Interface".