Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class with @Deprecated annotation means that all methods and fields automatically will be deprecated

Is Anyone knows if the class with @Deprecated annotation means that all methods and fields automatically will be deprecated..?

From JLS 9.6.3.6. @Deprecated

At lease eclipse is not showing methods as Deprecated for Deprecated class.

like image 727
Sumit Singh Avatar asked Jul 12 '13 12:07

Sumit Singh


3 Answers

No. If you do

@Deprecated
class Old {
     public void foo () {}
}

the when you'll reference that class:

new Old().foo()

only

Old

will be marked as deprecated.

like image 197
Funky coder Avatar answered Sep 20 '22 07:09

Funky coder


If say I have annotated the class VO with @Deprecated:

@Deprecated
class VO {
  private String name ;
  public void setName(String name) {
    this.name = name;
  }
  public String getName() {
    return name;
  }
}

Then :

VO v = new VO(); // warning here

v.getName(); // no warning

It means the class is deprecated. Hence the use of that type will show warning.

like image 27
AllTooSir Avatar answered Sep 19 '22 07:09

AllTooSir


Well, the spec says this:

A Java compiler must produce a deprecation warning when a type [..] whose declaration is annotated with the annotation @Deprecated is used (i.e. overridden, invoked, or referenced by name), [..]

So strictly speaking calling a method on an object of which the type is deprecated is not required to produce a warning (since "overridden" and "invoked" can only reference methods or constructors and the class is not referenced by name). Declaring that object, however must provide a warning.

However, nothing says that the compiler isn't allowed to provide more warnings. And in my opinion it is reasonable to assume that all methods of a deprecated class should be considered deprecated.

like image 35
Joachim Sauer Avatar answered Sep 19 '22 07:09

Joachim Sauer