• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Accessing private members

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This appears kind of quirky to me, just trying to understand what is happening here.
Should I not be able to access the newly created objects private variables?
I understand it is the same class but still they are private fields so how are they accessible?



Thanks,
 
Enthuware Software Support
Posts: 4884
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because private means private to the class and not to the object.
The code accessing a field is written in a class, so how would it work if it were private to an object? How would the compiler accept/reject the code because there is no object at compile time.
 
John O Sullivan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Anilprem wrote:Because private means private to the class and not to the object.
The code accessing a field is written in a class, so how would it work if it were private to an object? How would the compiler accept/reject the code because there is no object at compile time.



So objects just have access to private members of other objects of the same type..?
That seems like it has potential for issues..

 
Paul Anilprem
Enthuware Software Support
Posts: 4884
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>So objects just have access to private members of other objects of the same type..?
No, they don't just have access to private members. They have access to public, protectec, and default members also.

>That seems like it has potential for issues.
You could say that but I don't think it really is an issue because it is still the developer/owner of the class who decides what members should be accessed and/or modified in any of its methods. It is not like someone else is able to modify fields of object of some one else's class.

In any case, that is just how Java does it.

 
John O Sullivan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Anilprem wrote:>So objects just have access to private members of other objects of the same type..?
No, they don't just have access to private members. They have access to public, protectec, and default members also.


That's not quite what I meant by the 'just' but fair enough, haha.

Paul Anilprem wrote:>
>That seems like it has potential for issues.
You could say that but I don't think it really is an issue because it is still the developer/owner of the class who decides what members should be accessed and/or modified in any of its methods. It is not like someone else is able to modify fields of object of some one else's class.


Sure, I do see you're point, it's just that mistakes can be made over time and things exposed which shouldn't be.
It's just kind of surprising that it is this way, it had me wondering if I was missing something, hence the question.

Paul Anilprem wrote:>
In any case, that is just how Java does it.


So it is
 
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was intended all along that private members of a class be accessible from the whole of the class; as Paul said, only the people creating that class can access them. It makes it much easier to write the equals() method.Think how much more difficult that would be if private members were accessible only in their own object.
The above is a very basic and old‑fashioned way to write equals(); nowadays you would write this:-
 
I'm doing laundry! Look how clean this tiny ad is:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic