2

I don't know Java, and I started learning Scala. What are annotations and what are they for?

3 Answers 3

8

Annotations are meta-data attached to classes/methods/fields and can be used by frameworks to determine required functionality surrounding those classes/methods/fields.

Common uses include:

  1. marking methods as unit-test methods and for associated set-up/tear-down (e.g. JUnit and TestNG)
  2. marking classes and fields for persistence (e.g. Hibernate)
  3. marking classes and fields to be exposed as JMX beans

Annotations are a Java feature, and as such available in Scala as well as Java.

Sign up to request clarification or add additional context in comments.

3 Comments

Annotations are a Java feature with support in the JVM, to be sure, but that doesn't make them automatically a part of Scala.
In particular, since you can annotate things in Scala that you cannot annotate in Java, or that don't even exist in Java, I'm pretty sure that Scala Annotations are not just a simple 1:1 mapping to either Java Annotations or JVM Annotations.
I'm not making assertions that there's a one-to-one mapping. But I am saying that Java annotations are supported in the JVM and available in Scala. All the above fit into that category.
5

Annotations, in general, make it possible to associate information with a definition e.g. the definition of a class, method or variable. Annotations can be used by the compiler or accessed by other parts of your program e.g. in Java:

@SuppressWarnings("deprecation") void useADeprecatedMethod() { someObj.someDeprecatedMethod(); } 

Here, the @SuppressWarnings annotation tells the compiler not to issue a warning about useADeprecatedMethod's use of someDeprecatedMethod.

Other uses in the Java world include adding information to a class regarding how it maps to a relational database for use by OR mappers like Hibernate.

In Scala, a number of things which are keywords or marker interfaces in Java are implemented as annotations in Scala. e.g. @throws, @serializable.

Here is an example showing Scala and Java working together with the help on an annotation. Imagine we're going to define a Person class in Scala. When we come to use Person in Java, the Java programmer will expect us to have setName and getName methods as is the bean convention in Java. This can be achieved using the @BeanProperty annotation:

Person class in Scala:

class Person { @BeanProperty var name = "Joe Bloggs" } 

In Java:

public void printPerson(Person p) { // Scala added the getName method for us System.out.println(p.getName()); } 

Comments

3

One interesting aspect of Java annotations is control over their retention. When an annotation type (a Java class that implements the Annotation interface) is defined, one of its properties is its RetentionPolicy, one of:

  • SOURCE — Annotations are to be discarded by the compiler.
  • CLASS — Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.
  • RUNTIME — Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.