문제

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

도움이 되었습니까?

해결책

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 Unsafe.allocateInstance 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 ...)

다른 팁

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 .

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 {
    // ...
}

나는 그것이 가능한 것이 가능하다고 생각하지 않는다!계산 된 필드는 컨테이너 유형이 아닌 데이터를 기반으로합니다!

사용자 정의 코드 또는 워크 플로를 통해 해당해야합니다!또는 BOOL처럼 폴더인지 여부를 나타내는 추가 열이 있거나 해당 열에 계산 된 필드를 사용합니다

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top