Marker interface doesn't has any thing. It contains only interface declarations, then how it is handled by the JVM for the classes which implements this marker interface?
Can we create any new marker interfaces ?
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.
Java marker interface are useful if we have information about the class and that information never changes, in such cases, we use marker interface represent to represent the same. Implementing an empty interface tells the compiler to do some operations.
You can create your own custom marker interface in Java. Here is an example using custom Marker interface in Java. As we know marker interfaces define a type, that can be used with instanceof operator to test whether object is an instance of the specified type.
Your question should really be how does the compiler handle marker interfaces, and the answer is: No differently from any other interface. For example, suppose I declare a new marker interface Foo
:
public interface Foo {
}
... and then declare a class Bar
that implements Foo
:
public class Bar implements Foo {
private final int i;
public Bar(int i) { this.i = i; }
}
I am now able to refer to an instance of Bar
through a reference of type Foo
:
Foo foo = new Bar(5);
... and also check (at runtime) whether an object implements Foo
:
if (o instanceof Foo) {
System.err.println("It's a Foo!");
}
This latter case is typically the driver behind using marker interfaces; the former case offers little benefit as there are no methods that can be called on Foo
(without first attempting a downcast).
then how it is handled by the JVM for the classes which implements this marker interface?
Instances of class implementing a Java marker interface benefit from a specific behavior because some JDK classes or the HotSpot JVM provide a specific behavior for them.
For example take the Serializable
interface.
If you dig into ObjectOutputStream
and ObjectInputStream
you can see that the serialization/unserialization behavior are implemented in.
Here is a snippet of ObjectOutputStream.writeObject0()
invoking by ObjectOutputStream.writeObject()
that illustrates that :
public class ObjectOutputStream extends OutputStream implements ObjectOutput, ObjectStreamConstants {
...
private void writeObject0(Object obj, boolean unshared) throws IOException {
...
if (obj instanceof String) {
writeString((String) obj, unshared);
} else if (cl.isArray()) {
writeArray(obj, desc, unshared);
} else if (obj instanceof Enum) {
writeEnum((Enum<?>) obj, desc, unshared);
} else if (obj instanceof Serializable) {
writeOrdinaryObject(obj, desc, unshared);
} else {
if (extendedDebugInfo) {
throw new NotSerializableException(
cl.getName() + "\n" + debugInfoStack.toString());
} else {
throw new NotSerializableException(cl.getName());
}
}
...
}
}
For the Cloneable
interface, look at the Object.clone()
method and you will see that it refers a native method that applies the Cloneable
specification.
In the HotSpot source code, the src\share\vm\prims\jvm.cpp
, you can find the Object.clone()
implementation :
JVM_ENTRY(jobject, JVM_Clone(JNIEnv* env, jobject handle))
JVMWrapper("JVM_Clone");
Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
const KlassHandle klass (THREAD, obj->klass());
JvmtiVMObjectAllocEventCollector oam;
// I skip all the processing that you can read in the actual source file
...
return JNIHandles::make_local(env, oop(new_obj));
JVM_END
For this marker interface the behavior is not directly implemented in a JDK class but by the JVM itself but the general idea is the same.
Can we create any new marker interfaces ?
If you create your own marker interfaces, you should do as the JVM and the JDK classes do to handle built-in instances of class implementing a Java marker interface : that is adding code to handle specifically the instances of your marker interfaces.
The very good answer of Adamski
shows the general idea to do that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With