tags:

views:

77

answers:

1

how to create your own marker interface in java.how to notify the JVM to treat as special class? Can any one elaborate it.

Thanks in advance....

+6  A: 

You can't do anything like that with the JVM.

Well, you can, but you rarely want to do it. JVM agents can be 'plugged' in the JVM.

But marker interfaces are not used for that - they are used to mark classes that are eligible for something. Serializable for example is not checked in the JVM - it is checked by the ObjectOutputStream.

So you can create public interface MyMarker {} and use instanceof to verify whether a given class implements it, in your own logic.

However, since Java 1.5, the preferred way to do this is via an annotation (even if you use a jvm agent) -

public @interface MyMarker {..}

@MyMarker
public class MyClass { .. }

and then verify:

object.getClass().isAnnotationPresent(MyMarker.class);
Bozho