Saturday, August 21, 2004

Annotations - one of the many differences between .NET and Java 5.0

If you are a software engineer using both .NET(C#) and Java 5.0, there are some subtle differences between both metadata implementations - attributes/annotations. The first one you normally run into is the fact that Java's annotations are available at source level only by default. Try running the following code:

public @interface SomeAnnotation { String value();}

@SomeAnnotation("hello")
public class SomeClass {
public static void main(String[] args) {
System.out.println(SomeClass.class.isAnnotationPresent(SomeAnnotation.class));
}}


If you come from .NET land, you might expect the output to be "true", but what you actually get is "false". To make the annotation available at runtime, change SomeAnnotation to:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
public @interface
SomeAnnotation { String value();}

0 Comments:

Post a Comment

<< Home