This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Access to private members Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Access to private members" Watch "Access to private members" New topic
Author

Access to private members

vijayk kumar
Greenhorn

Joined: Jul 29, 2006
Posts: 18
Can the object refernce be used to access a private member?
If so in what situations?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

Only from code in the same class; any instance of class X can access the private members of any other instance of class X, but no code in any other classes can.


[Jess in Action][AskingGoodQuestions]
vijayk kumar
Greenhorn

Joined: Jul 29, 2006
Posts: 18
Yes , I get it .Now I will try with an example.
Pratibha Malhotra
Ranch Hand

Joined: Dec 21, 2003
Posts: 199
But is not illegal to happen. I mean private variables should only available for owner only.

Is it a bug or an expected behaviour.
[ July 31, 2006: Message edited by: Rewa Dev ]

~ Pratibha Malhotra<br /> <br />Sun Certified Java Programmer<br />SCEA 1.4 (In Progress)<br />~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />"Many of life's failures are people who did not realize how close they were to success when they gave up!!"
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
It's expected behaviour.

The reason to make fields private is so that code outside the class doesn't get coupled to implementation details.

With other words, the purpose of access modifiers is mainly to manage the static structure of the source code, not to manage runtime dependencies of objects.

Does that sound reasonable?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

Originally posted by Rewa Dev:

Is it a bug or an expected behaviour.


It is definitely expected behavior. Some languages (Java, C++, and others) have class-based encapsulation, in which the class is the level of protection. Others (Ruby is a good example) have object-based encapsulation, in which one object can't call the private members of another; this is rather rarer. Both have their advantages; note that writing comparison methods, clone methods, etc, can be difficult in object-based encapsulation systems.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access to private members
 
Similar Threads
when private is not so private
Making a class non extensible without using final
Explain....
How a subclass access to private of root class?
dynamic lookup