0% found this document useful (0 votes)
26 views

AJ Program Explaination

Program desc

Uploaded by

ayman patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

AJ Program Explaination

Program desc

Uploaded by

ayman patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Debugged Program:

import java.lang.annotation.*;
import java.lang.re ect.*;

// De ne the annotation
@Retention(RetentionPolicy.RUNTIME)
@interface myAnno {
String str();
int val();
}

// Class that uses the annotation


class Meta {
// Annotate the method
@myAnno(str = "This is the main method", val = 100)
public static void myMeth() {
System.out.println("Default value is " + 100);
Meta ob = new Meta();
try {
Class<?> c = ob.getClass(); // Use Class<?> instead of raw type Class
Method m = c.getMethod("myMeth");
myAnno anno = m.getAnnotation(myAnno.class); // Use the correct
annotation name
System.out.println(anno.str() + " " + anno.val()); // Add a space for better
formatting
} catch (NoSuchMethodException | SecurityException e) { // Handle
exceptions properly
e.printStackTrace();
}
}

public static void main(String[] args) {


myMeth();
}
}
fi
fl
General Overview

This Java program demonstrates how to de ne and use custom annotations,


and how to use re ection to access these annotations at runtime.

1. De ne a Custom Annotation: A special kind of marker that can be added


to classes, methods, or other program elements to provide metadata.
2. Apply the Annotation to a Method: Mark a method with the custom
annotation and provide speci c values for its elements.
3. Use Re ection to Read the Annotation: Retrieve and print the annotation
values during the program's execution.

Working

• @Retention(RetentionPolicy.RUNTIME): This tells Java that our annotation


should be available when the program is running, not just when we're
writing the code.
• @interface myAnno: This is how we de ne our custom annotation named
myAnno.
• String str() and int val(): These are the two pieces of information our
annotation will hold – a text message (str) and a number (val).

We attach our custom annotation myAnno to a method called myMeth in the


Meta class. We ll in the annotation with a message and a number.

@myAnno(str = "This is the main method", val = 100): This annotation is


attached to the myMeth method with the message "This is the main method"
and the number 100.

Re ection :
Re ection is like a tool that lets us look at the code and its annotations while the
program is running.

• Create an Object: Meta ob = new Meta(); creates an instance of the Meta


class.
• Get Class Object: Class<?> c = ob.getClass(); gets the Class object
representing the Meta class.
• Get Method: Method m = c.getMethod("myMeth"); retrieves the myMeth
method.
• Get Annotation: myAnno anno = m.getAnnotation(myAnno.class); retrieves
the myAnno annotation from the myMeth method.
fl
fi
fl
fl
fi
fl
fi
fi
fi
• Print Values: System.out.println(anno.str() + " " + anno.val()); prints the
values from the annotation.

Output :

Default value is 100


This is the main method 100

You might also like