I have gone through the following tutorial :

And after reading the above article, I feel, that it is not possible to write a Marker interface, because, how can you instruct compiler, that, what tag, it embed in the .class file for your Marker interface.

Please correct me, if I am wrong. cheers :)

Was it helpful?


here tag is hexa decimal code AC ED, which is added to the .class file of that class which implements Serializable interface. So, that JVM treats this class file in a special way (may be some heavy resource allocation work), because instance of this class might be serialized. For normal classes, it adds CA FE hex.

Aha!! I understand your confusion.

  • CA FE the magic number for a bytecode file; i.e. the file you get when you compile a class. The bytecode file for ANY class has this magic number, whether it is serializable or not serializable.

  • AC ED is the magic number a serialized Java object file; i.e. the file you serialize an instance of some serializable class.

You are mixing up two different concepts (classes and instances) and their respective representations.

So the answer to your question is ... of course you can write your own marker interfaces! There is nothing special to the compiler about a class that implements a marker interface.

However, it would be impossible to duplicate the implementation of Java object deserialization in pure Java. Object deserialization uses a backdoor (the Unsafe.allocateInstance method) to create objects without invoking their constructors. AFAIK, this method cannot be called from normal Java code. (And even if it can, it shouldn't be ...)


Of course you can write a marker interface. A marker interface is generally just a Interface with no methods at all (so any class could implement it).

You seem to think that marker interfaces have some magical properties that do something on their own. That's not the case. Instead some other code can react on the presence of the marker interface on some object and act differently when a class implements it. But the marker interface itself doesn't do anything .

package com.example;
interface MarkerInterface {}

Here you have one. Just copypaste it into com/example/, compile and use it!

Here's an usage example:

class SomeClass implements MarkerInterface {
    // ...

You cannot create a marker interface that will have meaning to the JVM, like the interface does. However you could create a marker interface that you check for in your own code using instanceof.

However using marker interfaces in this manner is generally discourage now that we have annotations. Marking class methods and fields in various ways for later processing at compile time using the Annotation Processing Tool (apt) or at runtime using reflection is what annotations were created for.

So rather than creating a marker interface and using it like so:

class MyClass implements MyMarkerInterface {

You should probably create an annotation and use it like so:

class MyClass {

Depends on what you understand as a Marker interface. But in general, you can use instanceof in your code to check if an instance implements a Marker interface and then do something with this instance...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow