Javalangannotationelementtype Example

Enumerations, Autoboxing, and Annotations (Metadata)

Enumerations, Autoboxing, and Annotations (Metadata)

Enumerations Ø In its simplest form, an enumeration is a list of named constants.

Enumerations Ø In its simplest form, an enumeration is a list of named constants. Ø An enumeration is created using the enum keyword. Ø A simple enumeration that lists various apple varieties // An enumeration of apple varieties. enum Apple { Jonathan, Golden. Del, Red. Del, Winesap, Cortland } Ø Once you have defined an enumeration, you can create a variable of that type. However, even though enumerations define a class type, you do not instantiate an enum using new. Instead, you declare and use an numeration variable in much the same way as you do one of the primitive types. Apple ap; ap = Apple. Red. Del; 2

Enumerations // An enumeration of apple varieties. enum Apple { Jonathan, Golden. Del, Red.

Enumerations // An enumeration of apple varieties. enum Apple { Jonathan, Golden. Del, Red. Del, Winesap, Cortland } class Enum. Demo { public static void main(String args[]) { Apple ap; ap = Apple. Red. Del; // Output an enum value. System. out. println("Value of ap: " + ap); System. out. println(); ap = Apple. Golden. Del; // Compare two enum values. if(ap == Apple. Golden. Del) System. out. println("ap contains Golden. Del. n"); // Use an enum to control a switch statement. switch(ap) { case Jonathan: System. out. println("Jonathan is red. "); break; case Golden. Del: System. out. println("Golden Delicious is yellow. "); break; case Red. Del: System. out. println("Red Delicious is red. "); break; case Winesap: System. out. println("Winesap is red. "); break; case Cortland: System. out. println("Cortland is red. "); break; } } } 3

Enumerations The values( ) and value. Of( ) Methods Syntax public static enum-type[ ]

Enumerations The values( ) and value. Of( ) Methods Syntax public static enum-type[ ] values( ) public static enum-type value. Of(String str) s The values( ) method returns an array that contains a list of the enumeration constants. s The value. Of( ) method returns the enumeration constant whose value corresponds to the string passed in str. The following program demonstrates the values( ) and value. Of( ) methods: // Use the built-in enumeration methods. // An enumeration of apple varieties. enum Apple { Jonathan, Golden. Del, Red. Del, Winesap, Cortland } class Enum. Demo 2 { public static void main(String args[]) { Apple ap; System. out. println("Here all Apple constants: "); // use values() Apple allapples[] = Apple. values(); for(Apple a : allapples) System. out. println(a); System. out. println(); // use value. Of() ap = Apple. value. Of("Winesap"); System. out. println("ap contains " + ap); } } 4

Enumerations Java Enumerations Are Class Types s As explained, a Java enumeration is a

Enumerations Java Enumerations Are Class Types s As explained, a Java enumeration is a class type. Although you don't instantiate an enum using new, it otherwise has much the same capabilities as other classes. Example program to demonstrate how it is similar to class // Use an enum constructor, instance variable, and method. enum Apple { Jonathan(10), Golden. Del(9), Red. Del(12), Winesap(15), Cortland(8); private int price; // price of each apple // Constructor Apple(int p) { price = p; } int get. Price() { return price; } } class Enum. Demo 3 { public static void main(String args[]) { Apple ap; // Display price of Winesap. System. out. println("Winesap costs " + Apple. Winesap. get. Price() + " cents. n"); // Display all apples and prices. System. out. println("All apple prices: "); for(Apple a : Apple. values()) System. out. println(a + " costs " + a. get. Price() + " cents. "); 5 } }

Enumerations Inherit Enum s Although you can't inherit a superclass when declaring an enum,

Enumerations Inherit Enum s Although you can't inherit a superclass when declaring an enum, all enumerations automatically inherit one: java. lang. Enum. § The ordinal( ) method obtains a value that indicates an enumeration constant's position in the list of constants. Syntax : final int ordinal( ) § The compare. To( ) method compare the ordinal value of two constants of the same enumeration syntax: final int compare. To(enum-type e) § the equals( ) method compare for equality an enumeration constant with any other object. The following program demonstrates the ordinal( ), compare. To( ), and equals( ) methods: // Demonstrate ordinal(), compare. To(), and equals(). // An enumeration of apple varieties. enum Apple { Jonathan, Golden. Del, Red. Del, Winesap, Cortland } class Enum. Demo 4 { public static void main(String args[]) { Apple ap, ap 2, ap 3; // Obtain all ordinal values using ordinal(). System. out. println("Here all apple constants" + " and their ordinal values: "); for(Apple a : Apple. values()) System. out. println(a + " " + a. ordinal()); ap = Apple. Red. Del; ap 2 = Apple. Golden. Del; ap 3 = Apple. Red. Del; System. out. println(); // Demonstrate compare. To() and equals() if(ap. compare. To(ap 2) < 0) System. out. println(ap + " comes before " + ap 2); if(ap. compare. To(ap 2) > 0) System. out. println(ap 2 + " comes before " + ap); if(ap. compare. To(ap 3) == 0) System. out. println(ap + " equals " + ap 3); System. out. println(); if(ap. equals(ap 2)) System. out. println("Error!"); if(ap. equals(ap 3)) System. out. println(ap + " equals " + ap 3); if(ap == ap 3) System. out. println(ap + " == " + ap 3); } } 6

Enumerations Another Enumeration Example import java. util. Random; // An enumeration of the possible

Enumerations Another Enumeration Example import java. util. Random; // An enumeration of the possible answers. enum Answers { NO, YES, MAYBE, LATER, SOON, NEVER } class Question { Random rand = new Random(); Answers ask() { int prob = (int) (100 * rand. next. Double()); if (prob < 15) return Answers. MAYBE; // 15% else if (prob < 30) return Answers. NO; // 15% else if (prob < 60) return Answers. YES; // 30% else if (prob < 75) return Answers. LATER; // 15% else if (prob < 98) return Answers. SOON; // 13% else return Answers. NEVER; // 2% } } class Ask. Me { static void answer(Answers result) { switch(result) { case NO: System. out. println("No"); break; case YES: System. out. println("Yes"); break; case MAYBE: System. out. println("Maybe"); break; case LATER: System. out. println("Later"); break; case SOON: System. out. println("Soon"); break; case NEVER: System. out. println("Never"); break; } } public static void main(String args[]) { Question q = new Question(); answer(q. ask()); } } 7

Type Wrappers s s Java uses primitive types (also called simple types), such as

Type Wrappers s s Java uses primitive types (also called simple types), such as int or double, to hold the basic data types supported by the language. Java provides type wrappers, which are classes that encapsulate a primitive type within an object. The type wrappers are Double, Float, Long, Integer, Short, Byte, Character, and Boolean. These classes offer a wide array of methods that allow you to fully integrate the primitive types into Java's object hierarchy. Character is a wrapper around a char. The constructor for Character is Character(char ch) Here, ch specifies the character that will be wrapped by the Character object being created. To obtain the char value contained in a Character object, call char. Value( ), shown here: char. Value( ) It returns the encapsulated character. Boolean is a wrapper around boolean values. It defines these constructors: Boolean(boolean bool. Value) Boolean(String bool. String) In the first version, bool. Value must be either true or false. In the second version, if bool. String contains the string "true" (in uppercase or lowercase), then the new Boolean object will be true. Otherwise, it will be false. To obtain a boolean value from a Boolean object, use boolean. Value( ), shown here: boolean. Value( ) It returns the boolean equivalent of the invoking object. 8

Type Wrappers s The Numeric Type Wrappers By far, the most commonly used type

Type Wrappers s The Numeric Type Wrappers By far, the most commonly used type wrappers are those that represent numeric values. These are Byte, Short, Integer, Long, Float, and Double. All of the numeric type wrappers inherit the abstract class Number declares methods that return the value of an object in each of the different number formats. These methods are shown here: byte. Value( ) double. Value( ) float. Value( ) int. Value( ) long. Value( ) short. Value( ) The following program demonstrates how to use a numeric type wrapper to encapsulate a value and then extract that value. // Demonstrate a type wrapper. class Wrap { public static void main(String args[]) { Integer i. Ob = new Integer(100); int i = i. Ob. int. Value(); System. out. println(i + " " + i. Ob); // displays 100 } } The process of encapsulating a value within an object is called boxing. Thus, in the program, this line boxes the value 100 into an Integer: Integer i. Ob = new Integer(100); The process of extracting a value from a type wrapper is called unboxing. For example, the program unboxes the value in i. Ob with this statement: int i = i. Ob. int. Value(); 9

Auto. Boxing s s s Autoboxing is the process by which a primitive type

Auto. Boxing s s s Autoboxing is the process by which a primitive type is automatically encapsulated (boxed) into its equivalent type wrapper whenever an object of that type is needed. Auto-unboxing is the process by which the value of a boxed object is automatically extracted (unboxed) from a type wrapper when its value is needed. // Program that demonstrate autoboxing/auto unboxing. class Auto. Box { public static void main(String args[]) { Integer i. Ob = 100; // autobox an int i = i. Ob; // auto-unbox System. out. println(i + " " + i. Ob); // displays 100 } } Auto boxing and Methods s autoboxing/unboxing might occur when an argument is passed to a method, or when a value is returned by a method. For example, consider this example: // Autoboxing/unboxing takes place with // method parameters and return values. class Auto. Box 2 { static int m(Integer v) { return v ; // auto-unbox to int } public static void main(String args[]) { Integer i. Ob = m(100); System. out. println(i. Ob); } } 10

Auto. Boxing Autoboxing/Unboxing Occurs in Expressions // Autoboxing/unboxing occurs inside expressions. class Auto. Box

Auto. Boxing Autoboxing/Unboxing Occurs in Expressions // Autoboxing/unboxing occurs inside expressions. class Auto. Box 3 { public static void main(String args[]) { Integer i. Ob, i. Ob 2; int i; i. Ob = 100; System. out. println("Original value of i. Ob: " + i. Ob); // The following automatically unboxes i. Ob, // performs the increment, and then reboxes // the result back into i. Ob. ++i. Ob; System. out. println("After ++i. Ob: " + i. Ob); // Here, i. Ob is unboxed, the expression is // evaluated, and the result is reboxed and // assigned to i. Ob 2 = i. Ob + (i. Ob / 3); System. out. println("i. Ob 2 after expression: " + i. Ob 2); // The same expression is evaluated, but the // result is not reboxed. i = i. Ob + (i. Ob / 3); System. out. println("i after expression: " + i); } } 11

Auto. Boxing Autoboxing/Unboxing Boolean and Character Values s Consider the following program: // Autoboxing/unboxing

Auto. Boxing Autoboxing/Unboxing Boolean and Character Values s Consider the following program: // Autoboxing/unboxing a Boolean and Character. class Auto. Box 5 { public static void main(String args[]) { // Autobox/unbox a boolean. Boolean b = true; // Below, b is auto-unboxed when used in // a conditional expression, such as an if. if(b) System. out. println("b is true"); // Autobox/unbox a char. Character ch = 'x'; // box a char ch 2 = ch; // unbox a char System. out. println("ch 2 is " + ch 2); } } Autoboxing/Unboxing Helps Prevent Errors // An error produced by manual unboxing. class Unboxing. Error { public static void main(String args[]) { Integer i. Ob = 1000; // autobox the value 1000 int i = i. Ob. byte. Value(); // manually unbox as byte !!! System. out. println(i); // does not display 1000 ! } } 12

Annotations(Meta. Data) s A new facility was added to Java that enables you to

Annotations(Meta. Data) s A new facility was added to Java that enables you to embed supplemental information into a source file. This information, called an annotation. s An annotation leaves the semantics of a program unchanged. s However, this information can be used by various tools during both development and deployment. Annotation Basics An annotation is created through a mechanism based on the interface. Example // A simple annotation type. @interface My. Anno { String str(); int val(); } s Notice the two members str( ) and val( ). Annotations consist solely of method declarations. s An annotation cannot include an extends clause. However, all annotation types automatically extend the Annotation interface. Thus, Annotation is a super-interface of all annotations. It is declared within the java. lang. annotation package. s here is an example of My. Anno being applied to a method: s // Annotate a method. @My. Anno(str = "Annotation Example", val = 100) public static void my. Meth() { //. . . } 13

Annotations(Meta. Data) Specifying a Retention Policy s Before exploring annotations further, it is necessary

Annotations(Meta. Data) Specifying a Retention Policy s Before exploring annotations further, it is necessary to discuss annotation retention policies. Aretention policy determines at what point an annotation is discarded. s Java defines three such policies, which are encapsulated within the java. lang. annotation. Retention. Policy enumeration. They are SOURCE, CLASS, and RUNTIME. s An annotation with a retention policy of SOURCE is retained only in the source file and is discarded during compilation. s An annotation with a retention policy of CLASS is stored in the. class file during compilation. However, it is not available through the JVM during run time. s An annotation with a retention policy of RUNTIME is stored in the. class file during compilation and is available through the JVM during run time. s A retention policy is specified for an annotation by using one of Java's built-in annotations: @Retention. Its general form is shown here: @Retention(retention-policy) My. Anno will be available to the JVM during program execution. @Retention(Retention. Policy. RUNTIME) @interface My. Anno { String str(); int val(); } 14

Annotations(Meta. Data) Obtaining Annotations at Run Time by Use of Reflection s s s

Annotations(Meta. Data) Obtaining Annotations at Run Time by Use of Reflection s s s Although annotations are designed mostly for use by other development or deployment tools, if they specify a retention policy of RUNTIME, then they can be queried at run time by any Java program through the use of reflection. Reflection is the feature that enables information about a class to be obtained at run time. The reflection API is contained in the java. lang. reflect package. The first step to using reflection is to obtain a Class object that represents the class whose annotations you want to obtain. Its general form is shown here: final Class get. Class( ) //It returns the Class object that represents the invoking object If you want to obtain the annotations associated with a specific item declared within a class, you must first obtain an object that represents that item. For example, Class supplies (among others) the get. Method( ), get. Field( ), and get. Constructor( ) methods, which obtain information about a method, field, and constructor, respectively. Method get. Method(String meth. Name, Class. . . param. Types) From a Class, Method, Field, or Constructor object, you can obtain a specific annotation associated with that object by calling get. Annotation( ). Its general form is shown here: Annotation get. Annotation(Class anno. Type) 15

Annotations(Meta. Data) Obtaining Annotations at Run Time by Use of Reflection s Here is

Annotations(Meta. Data) Obtaining Annotations at Run Time by Use of Reflection s Here is a program that assembles all of the pieces shown earlier and uses reflection to display the annotation associated with a method. import java. lang. annotation. *; import java. lang. reflect. *; // An annotation type declaration. @Retention(Retention. Policy. RUNTIME) @interface My. Anno { String str(); int val(); } class Meta {// Annotate a method. @My. Anno(str = "Annotation Example", val = 100) public static void my. Meth() { Meta ob = new Meta(); // Obtain the annotation for this method and display the values of the members. try { // First, get a Class object that represents this class. Class c = ob. get. Class(); // Now, get a Method object that represents this method. Method m = c. get. Method("my. Meth"); // Next, get the annotation for this class. My. Anno anno = m. get. Annotation(My. Anno. class); // Finally, display the values. System. out. println(anno. str() + " " + anno. val()); } catch (No. Such. Method. Exception exc) { System. out. println("Method Not Found. "); } } public static void main(String args[]) { my. Meth(); } } 16

Annotations(Meta. Data) A Second Reflection Example import java. lang. annotation. *; import java. lang.

Annotations(Meta. Data) A Second Reflection Example import java. lang. annotation. *; import java. lang. reflect. *; @Retention(Retention. Policy. RUNTIME) @interface My. Anno { String str(); int val(); } class Meta { // my. Meth now has two arguments. @My. Anno(str = "Two Parameters", val = 19) public static void my. Meth(String str, int i) { Meta ob = new Meta(); try { Class c = ob. get. Class(); // Here, the parameter types are specified. Method m = c. get. Method("my. Meth", String. class, int. class); My. Anno anno = m. get. Annotation(My. Anno. class); System. out. println(anno. str() + " " + anno. val()); } catch (No. Such. Method. Exception exc) { System. out. println("Method Not Found. "); }} public static void main(String args[]) { my. Meth("test", 10); }} 17

Annotations(Meta. Data) Obtaining All Annotations s s all annotations that have RUNTIME retention that

Annotations(Meta. Data) Obtaining All Annotations s s all annotations that have RUNTIME retention that are associated with an item by calling get. Annotations( ) on that item. It has this general form: Annotation[ ] get. Annotations( ) It returns an array of the annotations. get. Annotations( ) can be called on objects of type Class, Method, Constructor, and Field. // Show all annotations for a class and a method. import java. lang. annotation. *; import java. lang. reflect. *; @Retention(Retention. Policy. RUNTIME) @interface My. Anno { String str(); int val(); } @Retention(Retention. Policy. RUNTIME) @interface What { String description(); } @What(description = "An annotation test class") @My. Anno(str = "Meta 2", val = 99) class Meta 2 { @What(description = "An annotation test method") @My. Anno(str = "Testing", val = 100) public static void my. Meth() { Meta 2 ob = new Meta 2(); try { Annotation annos[] = ob. get. Class(). get. Annotations(); // Display all annotations for Meta 2. System. out. println("All annotations for Meta 2: "); for(Annotation a : annos) System. out. println(a); System. out. println(); 18

Annotations(Meta. Data) Obtaining All Annotations // Display all annotations for my. Method m =

Annotations(Meta. Data) Obtaining All Annotations // Display all annotations for my. Method m = ob. get. Class( ). get. Method("my. Meth"); annos = m. get. Annotations(); System. out. println("All annotations for my. Meth: "); for(Annotation a : annos) System. out. println(a); } catch (No. Such. Method. Exception exc) { System. out. println("Method Not Found. "); } } public static void main(String args[]) { my. Meth(); } } The output is shown here: All annotations for Meta 2: @What(description=An annotation test class) @My. Anno(str=Meta 2, val=99) All annotations for my. Meth: @What(description=An annotation test method) @My. Anno(str=Testing, val=100) 19

Annotations(Meta. Data) The Annotated. Element Interface • The methods get. Annotation( ) and get.

Annotations(Meta. Data) The Annotated. Element Interface • The methods get. Annotation( ) and get. Annotations( ) used by the preceding examples are defined by the Annotated. Element interface, which is defined in java. lang. reflect. This interface supports reflection for annotations and is implemented by the classes Method, Field, Constructor, Class, and Package. s In addition to get. Annotation( ) and get. Annotations( ), Annotated. Element defines two s other methods. The first is get. Declared. Annotations( ), which has this general form: Annotation[ ] get. Declared. Annotations( ) s It returns all non-inherited annotations present in the invoking object. The second is is. Annotation. Present( ), which has this general form: boolean is. Annotation. Present(Class anno. Type) s It returns true if the annotation specified by anno. Type is associated with the invoking object. It returns false otherwise. Using Default Values s You can give annotation members default values that will be used if no value is specified when the annotation is applied. type member( ) default value; Here, value must be of a type compatible with type. Here is @My. Anno rewritten to include default values: // An annotation type declaration that includes defaults. @Retention(Retention. Policy. RUNTIME) @interface My. Anno { String str() default "Testing"; int val() default 9000; } 20

Annotations(Meta. Data) Using Default Values Therefore, following are the four ways that @My. Anno

Annotations(Meta. Data) Using Default Values Therefore, following are the four ways that @My. Anno can be used: @My. Anno() // both str and val default @My. Anno(str = "some string") // val defaults @My. Anno(val = 100) // str defaults @My. Anno(str = "Testing", val = 100) // no defaults The following program demonstrates the use of default values in an annotation. import java. lang. annotation. *; import java. lang. reflect. *; // An annotation type declaration that includes defaults. @Retention(Retention. Policy. RUNTIME) @interface My. Anno { String str() default "Testing"; int val() default 9000; } class Meta 3 { // Annotate a method using the default values. @My. Anno() public static void my. Meth() { Meta 3 ob = new Meta 3(); // Obtain the annotation for this method // and display the values of the members. try { Class c = ob. get. Class(); Method m = c. get. Method("my. Meth"); 21

Annotations(Meta. Data) Using Default Values My. Anno anno = m. get. Annotation(My. Anno. class);

Annotations(Meta. Data) Using Default Values My. Anno anno = m. get. Annotation(My. Anno. class); System. out. println(anno. str() + " " + anno. val()); } catch (No. Such. Method. Exception exc) { System. out. println("Method Not Found. "); } } public static void main(String args[]) { my. Meth(); } } The output is shown here: Testing 9000 Marker Annotations Amarker annotation is a special kind of annotation that contains no members. Its sole purpose is to mark a declaration. The best way to determine if a marker annotation is present is to use the method is. Annotation. Present( ), which is a defined by the Annotated. Element interface. s import java. lang. annotation. *; s import java. lang. reflect. *; s // A marker annotation. s @Retention(Retention. Policy. RUNTIME) s @interface My. Marker { } s class Marker { s // Annotate a method using a marker. s @My. Marker s public static void my. Meth() { s Marker ob = new Marker(); 22

Annotations(Meta. Data) Marker Annotations try { Method m = ob. get. Class(). get. Method("my.

Annotations(Meta. Data) Marker Annotations try { Method m = ob. get. Class(). get. Method("my. Meth"); // Determine if the annotation is present. if(m. is. Annotation. Present(My. Marker. class)) System. out. println("My. Marker is present. "); } catch (No. Such. Method. Exception exc) { System. out. println("Method Not Found. "); } } public static void main(String args[]) { my. Meth(); } } The output, shown here, confirms that @My. Marker is present: My. Marker is present. Single-Member Annotations Asingle-member annotation contains only one member Here is an example that creates and uses a single-member annotation: import java. lang. annotation. *; import java. lang. reflect. *; // A single-member annotation. @Retention(Retention. Policy. RUNTIME) @interface My. Single { int value(); // this variable name must be value } 23

Annotations(Meta. Data) Single-Member Annotations class Single { // Annotate a method using a single-member

Annotations(Meta. Data) Single-Member Annotations class Single { // Annotate a method using a single-member annotation. @My. Single(100) public static void my. Meth() { Single ob = new Single(); try { Method m = ob. get. Class(). get. Method("my. Meth"); My. Single anno = m. get. Annotation(My. Single. class); System. out. println(anno. value()); // displays 100 } catch (No. Such. Method. Exception exc) { System. out. println("Method Not Found. "); } } public static void main(String args[]) { my. Meth(); } } As expected, this program displays the value 100. 24

Annotations(Meta. Data) Built-In Java Annotations used in java code s @Override s @Suppress. Warnings

Annotations(Meta. Data) Built-In Java Annotations used in java code s @Override s @Suppress. Warnings s @Deprecated Built-In Java Annotations used in custom annotations s @Target s @Retention s @Inherited s @Documented Understanding Built-In Annotations in java @Override s @Override annotation assures that the subclass method is overriding the parent class method. If it is not so, compile time error occurs. s Sometimes, we does the silly mistake such as spelling mistakes etc. So, it is better to mark @Override annotation that provides assurity that method is overridden. class Animal { void eat. Something(){System. out. println("eating something"); } } class Dog extends Animal{ @Override void eatsomething(){System. out. println("eating foods"); }//should be eat. Something } class Test. Annotation 1{ public static void main(String args[]) { Animal a=new Dog(); a. eat. Something(); }} 25

Annotations(Meta. Data) @Suppress. Warnings s @Suppress. Warnings annotation: is used to suppress warnings issued

Annotations(Meta. Data) @Suppress. Warnings s @Suppress. Warnings annotation: is used to suppress warnings issued by the compiler. import java. util. *; class Test. Annotation 2{ @Suppress. Warnings("unchecked") public static void main(String args[]){ Array. List list=new Array. List(); list. add("sonoo"); list. add("vimal"); list. add("ratan"); for(Object obj: list) System. out. println(obj); }} If you remove the @Suppress. Warnings("unchecked") annotation, it will show warning at compile time because we are using non-generic collection. @Deprecated annoation marks that this method is deprecated so compiler prints warning. It informs user that it may be removed in the future versions. So, it is better not to use such methods. class A{ void m(){System. out. println("hello m"); } @Deprecated void n(){System. out. println("hello n"); } } class Test. Annotation 3{ public static void main(String args[]){ A a=new A(); a. n(); }} s At Compile Time: Note: Test. java uses or overrides a deprecated API. Note: Recompile with -Xlint: deprecation for details. 26

Annotations(Meta. Data) Built-in Annotations used in custom annotations in java s @Target tag is

Annotations(Meta. Data) Built-in Annotations used in custom annotations in java s @Target tag is used to specify at which type, the annotation is used. s The java. lang. annotation. Element. Type enum declares many constants to specify the type of element where annotation is to be applied such as TYPE, METHOD, FIELD etc. Let's see the constants of Element. Type enum: Element Types Where the annotation can be applied TYPE class, interface or enumeration FIELD fields FIELDMETHOD methods CONSTRUCTOR constructors LOCAL_VARIABLE local variables ANNOTATION_TYPE annotation type PARAMETER s parameter Example to specify annoation for a class @Target(Element. Type. TYPE) @interface My. Annotation{ int value 1(); String value 2(); } 27

Annotations(Meta. Data) Built-in Annotations used in custom annotations in java import java. lang. annotation.

Annotations(Meta. Data) Built-in Annotations used in custom annotations in java import java. lang. annotation. *; import java. lang. reflect. *; @Retention(Retention. Policy. RUNTIME) @Target(Element. Type. METHOD) @interface My. Annotation{ int value(); } //Applying annotation class Hello{ @My. Annotation(value=10) public void say. Hello(){System. out. println("hello annotation"); } } //Accessing annotation class Test. Custom. Annotation 1{ public static void main(String args[])throws Exception{ Hello h=new Hello(); Method m=h. get. Class(). get. Method("say. Hello"); My. Annotation manno=m. get. Annotation(My. Annotation. class); System. out. println("value is: "+manno. value()); }} 28

Annotations(Meta. Data) Built-in Annotations used in custom annotations in java @Inherited By default, annotations

Annotations(Meta. Data) Built-in Annotations used in custom annotations in java @Inherited By default, annotations are not inherited to subclasses. The @Inherited annotation marks the annotation to be inherited to subclasses. @Inherited @interface For. Everyone {. . . }//Now it will be available to subclass also @interface For. Everyone {. . . } class Superclass{. . . } class Subclass extends Superclass{. . } @Documented The @Documented annotation is a marker interface that tells a tool that an annotation is to be documented. It is designed to be used only as an annotation to an annotation declaration. 29

swiftvance1996.blogspot.com

Source: https://slidetodoc.com/enumerations-autoboxing-and-annotations-metadata-enumerations-in-its/

0 Response to "Javalangannotationelementtype Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel