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 {
// ...
}
But,
You cannot create a marker interface that will have meaning to the JVM, like the
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:
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 {
}
---------------------------
The JRE wouldn't know anything about your marker
interface, so any special treatment would have to happen in the code you
write. Probably in sections such as
"if (someObject instanceof
ExampleMarker) { ... }".
You shouldn't create marker interfaces, though.
That's what annotations are used for these days.
No comments:
Post a Comment