Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use Java private methods (JDK class)?

Tags:

java

We used a private method, through reflection, of a JDK class. Is there a guarantee this will not change in a future release ? what is the QoS of a private method for a JDK class ?

We used ThreadPoolExecutor.isStopped() which is actually missing in Java 6 v18. Is there an easy way knowing the minimum jdk required or how to check when a class is changing in Java?

Comments are welcomed and yes we know better not using private methods :-)

like image 279
ic3 Avatar asked Jun 15 '26 14:06

ic3


2 Answers

We used a private method, through reflection, of a JDK class.

Very bad idea.

Is there a guarantee this will not change in a future release ?

No. It may change or disappear in any minor release.

what is the QoS of a private method for a JDK class ?

If there were a service-level agreement involved here, you would have forfeited it by using reflection to access a method you should not.

yes we know better not using private methods :-)

No, apparently you don't.

Why don't you use isTerminated() or isTerminating(). From the API doc it seems that one of these should do what you need.

like image 138
Michael Borgwardt Avatar answered Jun 18 '26 04:06

Michael Borgwardt


The public API defines the methods through which you can interact with objects. A good public API (and Java's is) will deprecate methods over time giving you a chance to upgrade your code to make use of the new and improved API. Often 'deprecated' methods will remain in the API for many releases, to provide backward compatibility.

This is not true of private methods, which could disappear in a minor update and no guarantees are made about their presence (or even the way they will operate). It sounds like an improper use of reflection, and indicates that you may want to create your own implementation of the functionality to guarantee it will be there in the future.

like image 34
spikeheap Avatar answered Jun 18 '26 02:06

spikeheap



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!