• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Private Access Modifier in Java API

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ??
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. It is used only inside its own class. You cannot see it in the published API documentation.
 
Rancher
Posts: 1044
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic