Java Reference
In-Depth Information
String name;
String firstName;
String lastName;
}
class UseEmployee
{
@SuppressWarnings("deprecation")
public static void main(String[] args)
{
Employee emp = new Employee();
emp.name = "John Doe";
}
}
Note
Asamatterofstyle,youshouldalwaysspecify
@SuppressWarnings
on
themostdeeplynestedelementwhereitiseffective.Forexample,ifyouwanttosup-
pressawarninginaparticularmethod,youshouldannotatethatmethodratherthanits
class.
Finally,
@SafeVarargs
annotations are useful for asserting that the body of the
annotated method or constructor does not perform potentially unsafe operations on its
variableumberofargumentsparameter.I'llhavemoretosayaboutthisannotationwhen
I present generics later in this chapter.
Declaring Annotation Types and Annotating Source Code
Beforeyoucanannotatesourcecode,youneedannotationtypesthatcanbeinstantiated.
Java supplies many annotation types as well as
Override
,
Deprecated
,
Sup-
pressWarnings
, and
SafeVarargs
. Java also lets you declare your own types.
Youdeclareanannotationtypebyspecifyingthe
@
symbol,immediatelyfollowedby
reservedword
interface
,followedbythetype'sname,followedbyabody.Forex-
Listing 3-41.
Declaring the
Stub
annotation type
public @interface Stub
{
}