简体   繁体   中英

How to let JVM know about custom marker interface in java

Is that possible to write our own marker interface in java. I am writing a code like

public interface MyMarker {

}

Is that a marker interface?

If it is possible then how can I let JVM know that this interface is my own created marker interface?

Yes, that's a marker interface. You would test whether an object "implemented" it as simply as:

if (x instanceof MyMarker)

For a particular class (instead of object) you'd want

if (MyMarker.isAssignableFrom(otherClass))

You should consider using annotations instead of marker interfaces though. They're not always direct replacements, but in many cases they're used for the same goals, and annotations are (IMO) cleaner.

we can't say that an interface which doesn't have any method is a marker interface. because the word "Marker" itself signifies the meaning that " marking something". so i say, the interface(whatever may be it's contents) by implementing which if a class gains some extra or specialized behavior like allow object to store into a persistence storage(Serializable) OR allow an object to make it's duplicate or raplica(Cloneable) OR allow a user to implement only one method(like run()) instead of implementing nearly 4 t0 5 methods in a subclass in thread programming(Runnable) .

these are the specialized behavior which can be gained by an object when it implements those interfaces which are nothing but called as MARKER INTERFACE.

CONCLUSION

a marker interface may or may not contain methods...

it can also be called as tagged interface,dummy interface,empty interface....

  1. "Generally " Marker Interfaces are used to signal compile or JVM, of the additional metadata that might be needed while dealing with the object of "marker type".

  2. For example, if a class uses java.io.Serializable interface, the compile actually generates the implementation code for that marker interface for the class which is needed at run time by JVM while storing/marshaling that object.

  3. So I don't see any practical utility of custom marker interface (as it wont mean anything for compiler/JVM which works on a finite set of marker interfaces in java)

A class which implements an interface it gain some special quality that Interfaces are called Marker or Tagged Interfaces.

ex: A class implements Runnable interface that class acts as a Thread. so it is called Marker Interface.

 A class implements java.io.Serializable interface that clsass act to send object class which is needed at run time by JVM while storing/marshaling that object.

 A Class implements java.lang.Clonable interface that object ready to cloning. so java.lang.Clonabel interface is called Marker or Taged interface.

Some people belive All empty interface are Marker Interfaces. But it is NOT CORRECT.

Because we take java.lang.Runnable Interface it is not Empty interface it contain method called void run().

in java API maximum all Marker Interfafes are Empty like java.io.Serializable.

Some Marker interfaces or not empty like java.lang.Runnable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM