How to mark a class as Deprecated?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Marking a class as deprecated is how you tell other developers, the compiler, and your future self that the type should no longer be used for new code. In Java, the correct approach is not just adding one annotation; the class should also explain what to use instead and whether removal is planned.
Use @Deprecated on the Class
At the Java language level, a deprecated class is marked with the @Deprecated annotation.
Three parts matter here:
- '
@Deprecatedtells the compiler and IDE that the class is obsolete.' - '
since = "2.4"records when you deprecated it.' - '
forRemoval = truetells callers that you intend to remove it in a later release.'
The annotation alone produces warnings in consuming code, which is useful because callers see the warning exactly where they still reference the old API.
If you compile code that still uses the class, Java reports a deprecation warning:
Compile with lint warnings enabled:
That gives you build-time visibility instead of relying on humans to notice a release note.
Add the Javadoc @deprecated Tag
A good deprecation includes migration guidance, not only a warning marker. In Java, the conventional place for that guidance is the Javadoc @deprecated tag.
The Javadoc tag is important because it answers the practical question every caller has: "What should I use instead?"
If you leave out the replacement path, you force every consumer to inspect source code or issue trackers to guess the migration target. That slows upgrades and increases the chance that teams keep using the deprecated class for another year.
Provide a Replacement Class
Deprecation works best when the alternative is already available and easy to adopt.
A small migration example makes the intent obvious:
This pattern keeps the warning actionable. A deprecation without a replacement is often just a delayed breaking change.
Decide Whether forRemoval Should Be True
forRemoval = true is a strong signal. Use it when you have a real removal plan and have confirmed that downstream users have time to react.
If you only want to discourage new usage but cannot remove the class soon, keep the deprecation but set forRemoval = false.
That distinction matters in libraries. Build tools, IDEs, and static analysis treat a pending removal more seriously, so setting it too early creates noise and unnecessary pressure.
Deprecation Is an API Contract, Not a Comment
Once a public class is deprecated, treat that decision as part of the API lifecycle. Keep the deprecation note accurate, mention it in release notes, and verify that examples and tests no longer teach the old type.
A practical rollout often looks like this:
- Add the replacement class.
- Mark the old class with
@Deprecatedand Javadoc guidance. - Update internal usages and documentation.
- Wait at least one stable release cycle.
- Remove the class only when callers have had a realistic migration window.
That sequence is much safer than deprecating and removing in the same release.
Common Pitfalls
- Using
@Deprecatedwithout a Javadoc@deprecatedmessage that names the replacement. - Setting
forRemoval = trueeven though the class will remain for several major releases. - Deprecating a class while tutorials, tests, or sample code still depend on it.
- Forgetting to enable compiler warnings, so the team never notices continuing usage.
- Deprecating a public class before the replacement API is stable enough for real workloads.
Summary
- Mark a Java class with
@Deprecated. - Add a Javadoc
@deprecatedtag that explains the migration path. - Use
sinceto record when the change happened. - Set
forRemovalonly when removal is genuinely planned. - Make deprecation useful by shipping a clear replacement and updating examples to match.
Related reading
- how to mark a message as persistent using spring-rabbitmq?
- How to measure service methods using spring boot 2 and micrometer
- How to migrate existing Spring project to Spring Boot
- How to mock a final class with mockito
- How to mock JWT authentication in a Spring Boot Unit Test?
- How to mock void methods with Mockito
- How to modify JsonNode in Java?
- How to modify request body before reaching controller in spring boot

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.