Next page | Contents page |

Annotations

Annotations are another innovation which started in Java 5. Beginning with an @ character, they can be placed like modifier keywords before classes, fields, methods, etc. They are not part of the program code proper but provide extra information to the compiler, JVM, or other processing tool. JDK 5 introduced 3 important annotations for programmers to use:

	@Override 
	public void mousePressed (MouseEvent ev) { ... }

tells the compiler that the method is intended to be overriding an inherited one; if it doesn't, a warning will be generated (perhaps you got the case of a letter wrong). This is very useful: do write @Override before every method that is meant to be overriding an inherited one.

	@Deprecated 
	public void methodSupercededbyABetterOne () { ... }

ensures that your API documentation shows clearly that you no longer want people to use this method. Quite useful.

	@SuppressWarnings ("deprecation")
	void useDeprecatedMethod () { jFrame.show (); }

The compiler's deprecation warning is suppressed. This is of very doubtful value and I would not recommend using it. It is an example of an annotation which takes a parameter - there are several different kinds of warnings that may be supressed (if you really must).

A useful feature is that anyone can write their own annotations (how to do that is not covered in this course). We will use some very good examples of that when we get to unit testing with JUnit.

Next page | Contents page |