What is @Deprecated annotation in Java

Java @Deprecated Annotation

@Deprecated is an annotation to mark method class or package as deprecated. We mark something deprecated to let developers know that the deprecated item can be removed in future.

Where @Deprecated can be used

  • Method can be marked @Deprecated.
  • Class can be marked with @Deprecated.
  • Package can be also marked deprecated with @Deprecated.

Effect of marking with @Deprecated

When we mark a method or class say @Deprecated then the IDE will mark the method with special style to alert about the deprecations. Also the generated Javadocs will covers deprecated items.


OldCalculator with @Deprecated
 * Sample Calculator which can add two numbers

public class OldCalculator {

	 * Add two number and return the value as Number
	 * @param a : First number
	 * @param b : Second number
	 * @return result of addition as Number
	 * @see Number
	public Number add(Number a, Number b) {
		return a.doubleValue() + b.doubleValue();

	@Deprecated(forRemoval = false, since = "1.1")
	public Number add(CalParameter a, CalParameter b) {
		return a.val.doubleValue() + b.val.doubleValue();


class CalParameter {
	Integer val;
In the above example class CalParameter is marked as @Deprecated. Where as method add is marked @Deprecated.

Documentation of @Deprecated

Following image shows Javadocs generated for a project, where some method, class and package are marked @Deprecated

When to use @Deprecated

When we have a better implementation of a method and want to let other developers use the new method rather then old method, we can mark old method @Deprecated. While refactoring code and marking @Deprecated to yet to be refactored code makes refactoring effective and easier.

No comments :

Post a Comment

Please leave your message queries or suggetions.