Commit 1a0e8e3
committed
feat: add example demonstrating @deprecated and @SuppressWarnings annotations
WHAT was added:
- Created `OldClass` with:
- `display()` method (normal, not deprecated).
- `show()` method marked with `@Deprecated` to indicate it should not be used in new code.
- Created `Annotations` class with a `main` method:
- Calls `show()` while suppressing compiler warnings using `@SuppressWarnings("deprecation")`.
KEY LEARNINGS:
1. @deprecated:
- Marks a method, class, or field as obsolete or unsafe to use.
- Compiler generates a warning when the deprecated element is used.
- Often used when APIs are replaced by newer versions but kept for backward compatibility.
2. @SuppressWarnings("deprecation"):
- Suppresses warnings triggered by using deprecated APIs.
- Should be used sparingly and only when you understand the implications.
- Example: maintaining legacy code while avoiding unnecessary compiler noise.
3. Best Practices:
- Prefer new methods over deprecated ones whenever possible.
- Document why suppression is necessary if you use `@SuppressWarnings`.
REAL-WORLD APPLICATIONS:
- ✅ Migrating legacy code: `@Deprecated` helps signal outdated APIs.
- ✅ Backward compatibility: Older methods remain available but marked as discouraged.
- ✅ Clean build pipelines: `@SuppressWarnings` avoids unnecessary warnings in stable legacy code.
- ✅ API evolution: Libraries like Spring, Java SDK, Android SDK use `@Deprecated` to phase out old methods gracefully.
RULE OF THUMB:
- Use `@Deprecated` to communicate API evolution to developers.
- Use `@SuppressWarnings` only when absolutely necessary, with clear documentation.
- Always migrate to newer APIs in the long run for better performance and maintainability.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 6a5f4df commit 1a0e8e3
1 file changed
+5
-35
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
| 1 | + | |
| 2 | + | |
32 | 3 | | |
33 | 4 | | |
| 5 | + | |
34 | 6 | | |
35 | | - | |
36 | | - | |
| 7 | + | |
37 | 8 | | |
38 | 9 | | |
39 | 10 | | |
40 | 11 | | |
41 | | - | |
42 | | - | |
| 12 | + | |
43 | 13 | | |
44 | 14 | | |
45 | 15 | | |
| |||
0 commit comments