• 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

How to represent the message ?

 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Could anyone represent the following message with code (From Java Rule Round-up)?
One object access a private variable of another object of the same class.
Thx!!
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code illustrates that:

When this code is compiled and run, it outputs 2
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Valentin --
hmmmm isn't that more a demonstration of encapsulation -- creating a public method to act as an accessor for a private variable?
[ February 13, 2003: Message edited by: Jessica Sant ]
 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much !!
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jess,
how would you then access a private variable of one object from within another object of the same class if not from within an instance method? Just asking...
What gets passed to the method is a reference variable to the other PrivateAccess object. Within the method pa1 tries to access the private member of pa2. I thought that's what the question was asking...
[ February 13, 2003: Message edited by: Valentin Crettaz ]
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Valentin Crettaz:
Jess,
how would you then access a private variable of one object from within another object of the same class if not from within an instance method? Just asking...
What gets passed to the method is a reference variable to the other PrivateAccess object. Within the method pa1 tries to access the private member of pa2. I thought that's what the question was asking...


I'll admit it -- not quite sure of what the question was getting at -- so instead of being helpful and providing an answer -- I decided to play Devil's Advocate and make sure your answer was correct.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The idea of the question is that if you have two objects of the same class that they can access each others private variables.
Valentin's example demonstrates this very well. pa1 has 1 in its variable. pa2 has two in its variable. But we can run a method against pa1 that reads and returns the contents of the private variable of pa2! Notice that the accessPrivate() method doesn't run a method to get the private variable. It simply takes the private varaible from the other object as if it was in its own object!
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll admit it -- not quite sure of what the question was getting at -- so instead of being helpful and providing an answer -- I decided to play Devil's Advocate and make sure your answer was correct.
No hard feelings
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic