• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private Access Modifier in Java API

 
Subhendu Dash
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any method present in Java API ,having private access modifier associated with it.If yes,then what is the significance of having such a private method.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subhendu Dash wrote:Is there any method present in Java API ,having private access modifier associated with it.If yes,then what is the significance of having such a private method.


Tell me, when do you declare a method private ??
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. It is used only inside its own class. You cannot see it in the published API documentation.
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subhendu Dash wrote:Is there any method present in Java API


Is the private stuff per definition not part of the API, the Application Programming Interface?
 
Subhendu Dash
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when we don't want any one to call our method directly or even know about our method..then we would declare it as private..but why i am asking this question is ..somewhere i read that we have private readObject() method somewhere in Java API (sry..not sure abt its package name)..so was wondering why to have a private method in ana API?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That has to do with Java's serialization mechanism. You can read about it in the API documentation of interface java.io.Serializable.

You can change the way Java handles serialization for your class by adding readObject() and writeObject() methods. These methods may have any access specifier, even private. It's a really strange way to design things, and I think the inventors of Java should have done this in a more logical way. For example, provide an interface that declares these methods, and have your class implement the interface if custom serialization is needed.
 
Subhendu Dash
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:
Subhendu Dash wrote:Is there any method present in Java API


Is the private stuff per definition not part of the API, the Application Programming Interface?


Yes it is..in fact it provides the best way to hide our implementation from the outside world and thus encapsulating our code..but here my question was about having a private method in an API.. which no one can call..so what's the use of it. And if the scenario is that some framework or JVM calls it internally then whats the point of mentioning it int the API..
 
Rajdeep Biswas
Ranch Hand
Posts: 207
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And a sample I found here at StackOverflow. If any methods exists as private in the API(there are!), they are strictly because Sun MS did not want us to use them from their accessible class, they gave candies as public for us. Those are used internally, ABSTRACTED from us....now you mihgt be recalling the stray abstraction definition!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subhendu Dash wrote:
Ivan Jozsef Balazs wrote:
Subhendu Dash wrote:Is there any method present in Java API


Is the private stuff per definition not part of the API, the Application Programming Interface?


Yes it is..in fact it provides the best way to hide our implementation from the outside world and thus encapsulating our code..but here my question was about having a private method in an API.. which no one can call..so what's the use of it. And if the scenario is that some framework or JVM calls it internally then whats the point of mentioning it int the API..


If I'm the author of the API, I'll create private methods just like anybody else. Methods that I will use internally but that are not intended to be called by anyone else.

The private readObject() and writeObject() in the serialization mechanism are oddballs, special cases. I'm not sure why they were done that way, but I'm sure at the time somebody had a reason that seemed to make sense. Even though no normal Java code can call them, the JVM can do whatever it wants. It has to be able to, otherwise nothing could ever get done. Presumably the JVM knows about the serialization mechanism, and as part of that process it has special rules that tell it to call those methods at the appropriate time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic