Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Java public API has private method?

Tags:

java-me

I was looking at java.security.BasicPermission API the other day. Why does it have a private method?

private void readObject(ObjectInputStream s) readObject is called to restore the state of the BasicPermission from a stream.

Sorry, I wasn't clear about what I asked. The class is just an example. There are many of them in Java library. All of them are read|write Object method. When they designed this API, why would they add a private method that an application can't use?

like image 358
user1132732 Avatar asked Dec 04 '25 13:12

user1132732


1 Answers

readObject is a used by the Java serialization framework when deserializing, to provide support for custom operations. Unlike most private methods, it usually wouldn't be called within the class itself - instead it's called by the framework / JVM, which obviously violates normal expectations somewhat.

See the docs for Serializable for more details.

like image 84
Jon Skeet Avatar answered Dec 08 '25 20:12

Jon Skeet