Can one object access a private variable of another object of the same class? Answer is given yes. But I couldn't understand the question. How can one object access other object's private variable? Please give me an example code.
Remember, private restricts access to the class -- not the instance.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Joined: Jun 01, 2007
Good answer, Marc. I had a couple of students confused by this just yesterday. The example code was:
The most common answer to this is that student's tend to gloss over the point that b needs to access a's private data. Some catch it and say that the code won't compile. Sadly, the ones with the sharper eyes are wrong. Access modifiers are at the class level, not the object level.
[ June 01, 2007: Message edited by: Bill Cruise ]
[ June 01, 2007: Message edited by: Bill Cruise ] [ June 01, 2007: Message edited by: Bill Cruise ]