beginner's question...
import javax.persistence.* ;
@Entity
@Table(name="CABIN")
publicclass Cabin{
privateint id;
private String name;
privateint deckLevel;
@Id
@GeneratedValue
@Column(name="CABIN_ID")
publicint getId( ){return id;}
publicvoid setId(int pk){ this.id = pk;}
@Column(name="CABIN_NAME")
public String getName( ){return name;}
publicvoid setName(String str){ this.name = str;}
@Column(name="CABIN_DECK_LEVEL")
publicint getDeckLevel( ){return deckLevel;}
publicvoid setDeckLevel(int level){ this.deckLevel = level;}
}
i was reading this from an O'Reilly book. What do the @ symbols mean? I didn't see a clear explanation of that... thanks!
[2033 byte] By [
pnandrusa] at [2007-11-27 4:28:21]

# 2
These @ symbols are used with Annotations. Annotations are decoratives of a class, which make it remotely accessible.
An example for an annotation:
HelpInfo.java
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface HelpInfo {
String description() default "Not explained";
String returns() default "void";
String arguments() default "void";
String by() default "Anonymous";
}
Copyright.java
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Copyright {
String value();
}
Test.java
import java.lang.reflect.Method;
@Copyright("Copyright 2007 - BeanDipper")
public class Test {
@HelpInfo(description="HelloWorld Creator",returns="String")
public String helloWorld() {
return "Hello World!";
}
public static void main(String arg[]) throws Exception {
Class myClass = Class.forName("Test");
System.out.printf("Class: %s %n", myClass.getSimpleName());
Copyright classCopy = (Copyright) myClass.getAnnotation(Copyright.class);
if(classCopy != null) {
System.out.printf("Copyright: %s %n", classCopy.value());
}
System.out.println("Methods:");
for(Method m : myClass.getMethods()) {
if(m.isAnnotationPresent(HelpInfo.class)) {
System.out.println("" + m.getName() + ":");
HelpInfo inf = m.getAnnotation(HelpInfo.class);
System.out.printf("Desc: %s %n", inf.description());
System.out.printf("Returns: %s %n", inf.returns());
System.out.printf("Params: %s %n", inf.arguments());
System.out.printf("By: %s %n", inf.by());
}else{
System.out.println("" + m.getName());
}
}
}
}
This example will scan the class, and then print the Copyright of the class - when present.
Then it'll iterate all methods, and print the name. When there's a HelpInfo present, it'll print it too.
Hope this example makes it clear.
# 5
> Sorry i think you don't understand my 'remote' then;
> not a remote machine, a remote class.
>
> Quote:
> http://java.sun.com/j2se/1.5.0/docs/guide/language/ann
> otations.html
>
> "This boilerplate could be generated automatically by
> a tool if the program were 揹ecorated?with
> annotations indicating which methods were remotely
> accessible. "
No no, I think you've misunderstood that article. The statement above is merely an example of a use of annotations, not the use of annotations. Think of cars. It is possible to use a car to kill a pheasant, but one would not define a car as "a device used for killing pheasants"