Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Strange program  RSS feed

 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.lang.annotation.*;
import java.lang.reflect.*;


@Retention(RetentionPolicy.RUNTIME)
@interface MyMarker { }

public class Marker {

// Annotate a method using a marker.
// Notice that no ( ) is needed.
@MyMarker
public static void myMeth() {
Marker ob = new Marker();

try {
Method m = ob.getClass().getMethod("myMeth");

// Determine if the annotation is present.
if(m.isAnnotationPresent(MyMarker.class))
System.out.println("MyMarker is present.");

} catch (NoSuchMethodException exc) {
System.out.println("Method Not Found.");
}
}

public static void main(String args[]) {
myMeth();
}
}

/************* can anyone explain me the program*****/
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to suggestions:
- Put code tags around the code to make it more readable
- Ask a more specific question. What do you expect it to do or what is it doing that you did not expect?
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is that @interface do there?

i heard it is injdk 1.5?
what is its use?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16029
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this piece of code, MyMarker is an annotation. Annotations are a new feature introduced in Java 5.

See The Java Tutorial - Annotations.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!