Reputation: 31
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?
Upvotes: 3
Views: 553
Reputation: 1500065
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.
Upvotes: 3
Reputation: 342
A private method is a method that cannot be accessed by the API, but is used internally to do something.
For example, take a real world example like a Microwave. It would have external user inputs like bake(), heat(), etc ... but a private internal function like cookFor(Time minutes, Temperature t).
So now the Microwave implementation is really simple,
public void bake() {
cookFor(45, 300);
}
public void heat() {
cookFor(5, 100);
}
etc. The reason we have functions is to write good procedural programs, and the private/public descriptor is used to encapsulate the class.
Upvotes: 0
Reputation: 62439
That method is probably called internally by one of the other public methods and should not be the concern of the API user.
Upvotes: 1