Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Private Variable access

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across this question in Java Ranch, "Can one object access a private variable of another object of the same class?". The site says this is possible. Can someone explain or justify this statement with a sample code? Thank you - Preetha
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the access modifiers are for the "class", not for the object.



Thus, member functions of the class can access all instance variables of an object of the same class.
[ July 22, 2006: Message edited by: Neelesh Bodas ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the partial implementation of a class which overrides Object's equals method:



Notice the expression:


The that.name refers to a private field name in another object (referenced by that). The type of that is the same class as the type of this (that is, AccessPrivate).

(All these this's and that's are making me dizzy)
[ July 22, 2006: Message edited by: Barry Gaunt ]
 
Preetha Ramaswamy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you friends for the clarification.

- Preetha
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess for the reason that Java allow an objectcan access private instant members of other objects of the same class is:

Equals() is one of the most essential method in Java. It requests that the object whose euqals() method is invoked can compares its attributes with the object which is compared to. The attributes to compare should not be restricted to exclude those with private access modifier, which is totally agains the common sense. Therefore, an object can access private instant members of other objects in the same class.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic