Saturday, April 26, 2014
Today in this tutorial I am going to discuss with the annotations that are already defined in the Java language. Some of them are used by the Java compiler and some are used by other annotations.
Annotations used by compiler and declared in java.lang package
- @Deprecated : This annotation is used to indicate that the element marked with it should not be used any longer. The compiler will generate warning if any program uses class, field or method marked with this annotation. It should also be documented with Javadoc annotation @deprecated. Many people make mistakes between the use of @ sign in annotation and javadoc comment. They are completely different and most importantly the annotation has upper-case while the Javadoc comment has a lower-case.
class DeprecationDemo{ /** * @deprecated * defines how to use deprecated annotation */ @Deprecated public void deprecatedMethod(){} }
@SupressWarnings("serial") class SupressWarningsDemo extends javax.swing.JPanel{ }It can also be used to supress other warnings like deprecated using "deprecation" if using any deprecated elements. Also unchecked warnings using "unchecked" while interfacing with legacy codes before the inclusion of generics.
There are certain other annotations called meta-annotations that apply to other annotations. They are declared in java.lang.annotation package
- @Retention : It is used to denote how the annotation should be stored. There are 3 ways
- RetentionPolicy.SOURCE : Retained at source level and ignored by compiler
- RetentionPolicy.CLASS : Retained by compiler at compile-time but ignored by JVM
- RetentionPolicy.RUNTIME : Used by JVM during run-time.
- @Documented : This annotation is used to indicate that whenever the particular annotation is used it should be documented using Javadoc tool. By default they are not documented
- @Target : It is used to indicate which kind of Java elements it can be applied.
- ElementType.ANNOTATION : Can be applied to annotation type
- ElementType.CONSTRUCTOR : Can be applied to constructors
- ElementType.FIELD : Can be applied to a field
- ElementType.LOCAL_VARIABLE : Can be applied to a local variable
- ElementType.METHOD : Can be applied to a method
- ElementType.PACKAGE : Can be applied to a package declaration
- ElementType.PARAMETER : Can be applied to a parameter of a method
- ElementType.TYPE : Can be applied to any element of a class.
- @Inherited : It indicates that the annotation type can be inherited from the super-class. When the user queries the annotation type and the class has no annotation for this type, the class' superclass is queried for the annotation type. This annotation applies only to class declarations.
- @Repeatable : This annotation has been included in Java 8. It indicates that the annotation marked with this can be apllied more than once to the same declaration.
Labels:Annotation,Java 8
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment