This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Private Access Modifier in Java API Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Private Access Modifier in Java API" Watch "Private Access Modifier in Java API" New topic
Author

Private Access Modifier in Java API

Subhendu Dash
Greenhorn

Joined: Mar 24, 2011
Posts: 18
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

Joined: Apr 21, 2012
Posts: 216

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 ??


OCPJP 6 86%
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38077
    
  22
Yes. It is used only inside its own class. You cannot see it in the published API documentation.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
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

Joined: Mar 24, 2011
Posts: 18
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Subhendu Dash
Greenhorn

Joined: Mar 24, 2011
Posts: 18
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

Joined: Mar 26, 2012
Posts: 186

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!


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Private Access Modifier in Java API
 
Similar Threads
JQPlus??
can somebody explain this?
main(String arr[])
Overloading Signature
Default constructor