Saturday, August 8, 2015

Annotations in JAVA

1.These generally inserted in your source code.
2.these may processed by Tool at compiletime/runtime/deployment time
3.these can be applied to Classes,Fields,Methods and other program elements
4.Annotations Do not end with ";". but every statement in java ends with ";". And more over these are like modifiers in java. So we have to keep these before class/method/variable.
5.Remember java comments are not part of javaprogram, but annotations are part of java program
6.
If you don't want to get warnings about the variables or methods which you didn't use in  in your code then you will get the warnings. So to avoid those we use "@SuppressWarnings" in front of identifier.
identifier nothing but ( class /method/variable....etc)

Ex:1
@SuppressWarnings
class A {}
If you apply @SuppressWarnings at beginning of class, that means it's also applied for all your methods and variables also.

Ex:2
class A
{
int x;
@SuppressWarnings
void f1()
{}
void f2()
{}

}

If you apply @SuppressWarnings at beginning of method, that means it's also applied to that method only. But not for f2() method
Ex:3

class A
{
@SuppressWarnings
int x;

int y;
void f1(){}
void f2(){}

}
If you apply @SuppressWarnings at beginning of x variable, that means it's applied to that 'x' variable only but not remaining 'y and f1() and f2() methos'.

7.
@SuppressWarnings(all)----handles all exceptions
@SuppressWarnings(unused,unchecked)----we can combine like this multible elements
@SuppressWarnings(unused)-warnings related to unused variables
@SuppressWarnings(fallthrough)---warnings related to missing breaks related to switch statement
@SuppressWarnings(hiding)----warnings related to locals that hide variables
@SuppressWarnings(deprecation)----warnings related to deprecation