views:

498

answers:

5

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

+5  A: 

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 .

Joachim Sauer
so marker interface is just a simple interface without method declarations. And, weather a class implements a Marker interface (Serializable) or any normal interface(with method declarations), no special hexa-decimal code is added in class file. But, article says, that, in case we implement the Serializable interface, AC ED code is added to the class file.
rits
No, you mis-read the article. The AC ED is a magic marker **only** in the serialized data! It's **not** written to the `.class` file.
Joachim Sauer
@Joachim - Thanx for this nice explanation. :)
rits
A: 

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

Frank Grimm
I understand that for marker interfaces, compiler (javac.exe) adds a special code in the .class file. So, accordingly, it's not possible to write a Marker interface. Plz clarify. :)
rits
As Joachim Sauer said in his answer, marker interfaces are ordinary Java interfaces which are empty (do not contain method declarations).The Serializable marker interface is something special, right, since the compiler adds a default serialization implementation.But nothing prevents you from writing your own marker interfaces that are helpful at runtime (e.g. instanceof).Anyway, I think that since Java provides support for annotations now, they should be used instead of marker interfaces.
Frank Grimm
+2  A: 
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 {
    // ...
}
BalusC
+4  A: 

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 java.sun.misc.Unsafe.allocateInstance(...) native 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 ...)

Stephen C
Thnx for correcting me Mr. Stephen. But, this raises one more confusion. If Serializable is a very normal interface (But Marker, no members, no declaration). Then, why a class need to be implements Serializable to write a object. Does jvm checks something like (x instanceof Serializable) before serializing or deserializing.
rits
@rits - not the JVM. The source code is at http://www.docjar.com/html/api/java/io/ObjectOutputStream.java.html - see line 1166.
Stephen C
@Stephen - i got it. :)
rits
+1  A: 

You cannot create a marker interface that will have meaning to the JVM, like the java.io.Serializable 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:

@MyAnnotation
class MyClass {
}
Tendayi Mawushe