Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this possible to write a Marker interface

I have gone through the following tutorial :

http://www.javaworld.com/community/node/2915

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 :)

like image 200
mogli Avatar asked Jan 27 '10 12:01

mogli


People also ask

Can we make marker interface in Java?

Java has many built-in marker interfaces, such as Serializable, Cloneable, and Remote. Let's take the example of the Cloneable interface. If we try to clone an object that doesn't implement this interface, the JVM throws a CloneNotSupportedException.

Is a marker an interface?

It is an empty interface (no field or methods). Examples of marker interface are Serializable, Cloneable and Remote interface. All these interfaces are empty interfaces.

Why marker interface has no methods?

Marker Interfaces in Java have special significance because of the fact that they have no methods declared in them which means that the classes implementing these interfaces don't have to override any of the methods. A few of the marker interfaces already exist in the JDK like Serializable and Cloneable.

What can we use instead of marker interface?

Instead of marker interface, Java 5 provides the annotations to achieve the same results. It allows flexible metadata capability. Therefore, by applying annotations to any class, we can perform specific action.


3 Answers

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

like image 166
Stephen C Avatar answered Nov 10 '22 08:11

Stephen C


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 .

like image 30
Joachim Sauer Avatar answered Nov 10 '22 08:11

Joachim Sauer


package com.example;
interface MarkerInterface {}

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

Here's an usage example:

class SomeClass implements MarkerInterface {
    // ...
}
like image 29
BalusC Avatar answered Nov 10 '22 09:11

BalusC