Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java Reflection question

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Reflection question

    @MyAnnotation(name="someName",  value = "Hello World")
    public class TheClass {
    }
     
     
     
     
    public @interface MyAnnotation {
    	String   value();
            String   name();
    }
     
     
     
    import java.lang.annotation.Annotation;
     
    public class Test {
     
    	public static void main(String[] args) {		
    		Class aClass = TheClass.class;
    		Annotation annotation = aClass.getAnnotation(MyAnnotation.class);		
     
    		if(annotation instanceof MyAnnotation){
    		    MyAnnotation myAnnotation = (MyAnnotation) annotation;
    		    System.out.println("name: " + myAnnotation.name());
    		    System.out.println("value: " + myAnnotation.value());
    		}
    	}
     
    }

    I'm expecting output:
    name: someName
    value: Hello World

    but I get nothing.

    --- Update ---

    Removing the if loop throws NullPointerException which leads me to belive that annotation is not assigned.

    --- Update ---

    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
     
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyAnnotation {
    	String   value();
            String   name();
    }

    This fixed it.

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Java Reflection question

    Why you need @ ?
    Whatever you are, be a good one

Similar Threads

  1. Reflection
    By lightOfDay in forum Object Oriented Programming
    Replies: 6
    Last Post: August 26th, 2012, 08:35 PM
  2. [SOLVED] Java Reflection, laoding external classes and casting
    By ashenwolf in forum Java Theory & Questions
    Replies: 3
    Last Post: May 10th, 2011, 12:33 PM
  3. Reflection and finalization
    By Dastagiri in forum Java Theory & Questions
    Replies: 1
    Last Post: July 6th, 2010, 11:20 PM
  4. [SOLVED] java Reflection Question - I am lost.
    By prain in forum Java Theory & Questions
    Replies: 3
    Last Post: May 13th, 2010, 02:43 PM
  5. Generics and Reflection
    By Kassiuz in forum Collections and Generics
    Replies: 3
    Last Post: March 15th, 2010, 09:32 AM

Tags for this Thread