{"id":1683,"date":"2011-03-16T11:50:19","date_gmt":"2011-03-16T06:20:19","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1683"},"modified":"2011-03-16T11:50:19","modified_gmt":"2011-03-16T06:20:19","slug":"reading-and-writing-custom-annotation","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/reading-and-writing-custom-annotation\/","title":{"rendered":"Reading and Writing Custom Annotation"},"content":{"rendered":"<p><a title=\"Meta Annotation in JAVA\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/meta-annotation-annotate-other-annotation\/\" target=\"_blank\">We have already discussed the basics of annotation and Meta Annotation in JAVA<\/a>. \u00a0In this article i will explain how to read and write the custom annotation in JAVA.<\/p>\n<p>To define custom Annotation, keyword <strong>interface <\/strong>is used preceded by @. Like interface, it also has the method declaration.<br \/>\nTo make topic easy, lets I want to create the annotation CopyRight<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.G2.Annotations;\nimport java.lang.annotation.Retention;\nimport java.lang.annotation.RetentionPolicy;\n\n@Retention(value=RetentionPolicy.RUNTIME)\npublic @interface CopyRight {\n\tpublic String owner();\n\tpublic String location();\n\tpublic String year();\n}\n<\/pre>\n<p><!--more-->To use this annotation in our class, we should use like:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.G2.Annotations;\n\n@CopyRight(location=&quot;Mumbai&quot;,owner=&quot;Jitendra Zaa&quot;,year=&quot;2011&quot;)\npublic class TestAnnotations {\n}\n<\/pre>\n<p><strong>Annotation can also have the default values<\/strong> while declaration.<br \/>\nExample:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.G2.Annotations;\nimport java.lang.annotation.Retention;\nimport java.lang.annotation.RetentionPolicy;\n\n@Retention(value=RetentionPolicy.RUNTIME)\npublic @interface CopyRight  {\n\tpublic String owner() default &quot;Jitendra Zaa&quot;;\n\tpublic String location() default &quot;Mumbai&quot;;\n\tpublic String year() default &quot;2011&quot;;\n}\n<\/pre>\n<p><strong>Program to read the Annotations:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.G2.Annotations;\n\nimport java.lang.annotation.Annotation;\n\n@CopyRight(location=&quot;Mumbai&quot;,owner=&quot;Jitendra Zaa&quot;,year=&quot;2011&quot;)\npublic class TestAnnotations {\n\n\t\/**\n\t * @param args\n\t *\/\n\tpublic static void main(String&#x5B;] args) throws ClassNotFoundException {\n\t\tTestAnnotations obj = new TestAnnotations();\n\t\tobj.display();\n\t}\n\n\tpublic void display() throws ClassNotFoundException\n\t{\n\t\tAnnotation&#x5B;] annotations = TestAnnotations.class.getAnnotations();\n\t\tSystem.out.println(annotations.length);\n\t\tfor(Annotation a : annotations)\n\t\t{\n\t\t\tCopyRight r = (CopyRight)a;\n\t\t\tSystem.out.println(&quot;Location Value in Annotation : &quot;+r.location());\n\t\t\tSystem.out.println(&quot;Owner Value in Annotation : &quot;+r.owner());\n\t\t\tSystem.out.println(&quot;Year Value in Annotation : &quot;+r.year());\n\t\t}\n\n\t}\n}\n<\/pre>\n<p>Output:<\/p>\n<blockquote><p>1<br \/>\nLocation Value in Annotation : Mumbai<br \/>\nOwner Value in Annotation : Jitendra Zaa<br \/>\nYear Value in Annotation : 2011<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Article on Reading and Writing Custom Annotation with example<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[3],"tags":[36],"class_list":["post-1683","post","type-post","status-publish","format-standard","hentry","category-java","tag-annotation"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1676,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/meta-annotation-annotate-other-annotation\/","url_meta":{"origin":1683,"position":0},"title":"Meta Annotation &#8211; annotate other annotation","author":"Jitendra","date":"March 16, 2011","format":false,"excerpt":"What is Meta Annotation and its usage in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Inbuilt Annotation and Meta Annotations in JAVA","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Inbuilt-Annotation-and-Meta-Annotations-in-JAVA.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1959,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/create-servlet-using-annotation\/","url_meta":{"origin":1683,"position":1},"title":"Create Servlet using Annotation &#8211; Servlet 3.0","author":"Jitendra","date":"April 13, 2011","format":false,"excerpt":"Create the Servlet without any deployment descriptor. Using Annotations","rel":"","context":"In &quot;Servlet&quot;","block_context":{"text":"Servlet","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/servlet\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1655,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/introduction-to-annotation-in-java\/","url_meta":{"origin":1683,"position":2},"title":"Introduction to Annotation in JAVA","author":"Jitendra","date":"March 14, 2011","format":false,"excerpt":"Introduction to Annotation in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1851,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/struts\/dynamic-results-in-struts-2\/","url_meta":{"origin":1683,"position":3},"title":"Dynamic Results in Struts 2","author":"Jitendra","date":"March 30, 2011","format":false,"excerpt":"Example and sourcecode of creating Dynamic Results in Struts 2","rel":"","context":"In &quot;Struts&quot;","block_context":{"text":"Struts","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/struts\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1764,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/read-xml-file-using-sax-parser-in-java\/","url_meta":{"origin":1683,"position":4},"title":"Read XML File using SAX Parser in JAVA","author":"Jitendra","date":"March 22, 2011","format":false,"excerpt":"Example of Reading XML File using SAX Parser in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"SAX Parser","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/SAX-Parser.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1612,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/serialization-marshalling-deflating-in-java\/","url_meta":{"origin":1683,"position":5},"title":"Serialization \/ Marshalling \/ Deflating in JAVA","author":"Jitendra","date":"March 9, 2011","format":false,"excerpt":"Concept of Serialization \/ Marshalling \/ Deflating in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Serialization ,Marshalling ,deflating, DeSerialization ,UnMarshalling ,inflating","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Serialization-Marshalling-deflating-DeSerialization-UnMarshalling-inflating.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1683"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1683\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}