0

In my practice as a Java programmer, I would have found the methods provided by classes such as sun.reflect.Reflection or sun.misc.Unsafe very useful in some cases, for example the ability to write caller-sensitive code. However, the use of sun.reflect and related packages by application code is strongly advised against as it is not public API. What are the reasons that this functionality is not added to the Java Standard Library? Sometimes I get the impression that the Java API developers mistrust a programmer's ability to consider potential risks of these methods and make a conscious decision on when their use is reasonable.

Note: My question is not a duplicate of 'It is a bad practice to use Sun's proprietary Java classes?' There, the question is, why these classes should not be used. My question is why they are not added to the API which is 'good practice to be used'.

Beethoven
  • 375
  • 1
  • 8
  • 2
    [It is a bad practice to use Sun's proprietary Java classes?](http://stackoverflow.com/questions/1834826/it-is-a-bad-practice-to-use-suns-proprietary-java-classes) – Pshemo Sep 05 '16 at 17:04

1 Answers1

0

It's not only because they don't trust the developer. Private APIs can be changed freely, since developers like you and I haven't written code that depends on them behaving in well specified ways. Java has a strong concept of backwards compatibility, so the public API will not change in breaking ways, but the people working on the JDK can modify and enhance the private API without worrying about breaking existing systems. This allows you to update the Java versions on your production systems relatively safely too, instead of having to keep let's say 1.5 installed just because you wrote your program in 1.5.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • Indeed, therefore I wonder why these classes cannot be _added_ to the well-documented, backwords-compatible, public API. – Beethoven Sep 05 '16 at 17:15
  • They sometimes are (reflection is there after all), but it makes harder for the JDK developers to provide a stable API if they give too powerful tools to developers. You can deadlock your app a lot easier with `Unsafe.park()` than with the standard API. – Kayaman Sep 05 '16 at 17:21