Is It Wrong to Use Deprecated Methods or Classes in Java

Is it wrong to use Deprecated methods or classes in Java?

1. Is it wrong to use Deprecated methods or classes in Java?

From the definition of deprecated:

A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists.

The method is kept in the API for backward compatibility for an unspecified period of time, and may in future releases be removed. That is, no, it's not wrong, but there is a better way of doing it, which is more robust against API changes.

2. What if I don't change any method and run my application with warnings that I have, will it create any performance issue.

Most likely no. It will continue to work as before the deprecation. The contract of the API method will not change. If some internal data structure changes in favor of a new, better method, there could be a performance impact, but it's quite unlikely.


The funniest deprecation in the Java API, is imo, the FontMetrics.getMaxDecent. Reason for deprecation: Spelling error.

Deprecated. As of JDK version 1.1.1, replaced by getMaxDescent().

Can I use deprecated classes?

From the documentation:

A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code.

So you can use some deprecated methods but it won't be the best practice because there are better alternative exists(but in some cases this can even be dangerous)

If something is deprecated, can I still use it?

Essentially deprecated is a warning to you as a developer that while the method/class/whatever is there and works it is not the best way to do it. Either a newer and better alternative is available or (sometimes) there is something subtly broken about it that makes it not advisable to use.

You can still use deprecated things - but you should look to see why they are deprecated first and see if the new way of doing things is better for you.

In theory deprecated things can or will in future be removed. That rarely happens (some Java libraries have deprecated stuff in them that is more than ten years old) but is a risk you should be aware of.

What are the technical reasons for why one shouldn't use deprecated methods or classes?

There are three good reasons to deprecate an API, according to Java documentation:

  • It is insecure, buggy, or highly inefficient
  • It is going away in a future release
  • It encourages bad coding practices

I would expect a comment in the Javadoc to explain which of the three is applicable for the deprecated concerned, and without knowing which API you're referring to, I cannot get more specific than that.

What problems arise if you use deprecated methods/functions in Java?

Some potential problems are:

  • Methods may cease to exist (this has never been the case in practice, but by the official definition, deprecated methods may be eliminated from future Java)
  • Serious programming errors may occur due to fatal flaws in deprecated methods (e.g. System.runFinalizersOnExit and its evil twin Runtime.runFinalizersOnExit)
  • Data corruption can occur due to inherently unsafe deprecated methods (e.g. Thread.stop)

References

  • java.sun.com Glossary

    • deprecation: Refers to a class, interface, constructor, method or field that is no longer recommended, and may cease to exist in a future version.
  • Language guide/How and When to Deprecate APIs
  • Annotation Type Deprecated API

Related questions

  • Is it wrong to use Deprecated methods or classes in Java?
  • Difference between a Deprecated and Legacy API?

What's the effective risk of using deprecated methods on Android platform?

One major risk is if Google stops the support for the deprecated methods in future version of their OS/firmware, your app which is based on deprecated methods will have undefined behaviour / it won't run at all. Note that the reason a company deprecates some methods/classes is either because of bugs/a better alternative exists.

Is it bad practice to override a deprecated method?

When it's a class that you're extending, it's ideally better to avoid overriding a deprecated method, as in a future release when/if that is removed, and you need to upgrade to the newer version of the library, you will have to rework with the removed method which was deprecated.

If in your instance this is the JFrame class that you're extending, and you intend to override the show() method, you can instead override the setVisible(boolean b) method (doc) which is the replacement for the show() method as mentioned in the javadoc.

Also, it is not advisable to override a base class method and entirely change its function, as

  • You cannot use the method for it's original use-case
  • The method's intent becomes misleading and makes no sense to override the method, when you can actually create a new method which clearly indicates its function

How to replace deprecated class method?

Drawbacks to use deprecated methods is that when you'll migrate to the newer version of library, the method may be gone completely and you'll have to rewrite your code. Another thing is that usually there are reasons for library developers to deprecate methods - either they are ineffective, or lead to bad design, or (most common) have analogs.

WebMvcConfigurer according to Docs has default method addArgumentResolvers(java.util.List<HandlerMethodArgumentResolver> resolvers). If you can't use it, check that your project supports Java 8.

Example how it works:

DefaultMethods.java

public interface DefaultMethods {   default void example(){
System.out.println("default method call"); } }

DefalutMethodsTest.java

public class DefalutMethodsTest implements DefaultMethods{
public static void main(String[] args) {
new DefalutMethodsTest().example();
}
@Override
public void example() {
System.out.println("implementer call");
DefaultMethods.super.example();
}
}

Output:

implementer call
default method call


Related Topics



Leave a reply



Submit