This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Explain this!! Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Explain this!!" Watch "Explain this!!" New topic
Author

Explain this!!

Sapna Korecherla
Greenhorn

Joined: Jul 07, 2002
Posts: 10
Hi,
I found this question in javaranch mock exam.
Can one object access a private variable of another object of the same class?
The Answer is YES. though the description was given, I could not understand it. I mean it was not clear.
Anyone! explain this!!
Sapna


Sapna
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9033
    
  10
Please?

Politeness will get you a lot farther than demands.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1865
hi sapna
all the scopes like Private, Protected etc are "LEXICAL". meaning, they are textual. they ARE NOT related to objects we create.
so, if i have private int x in my class C then i can refer to variable x withing the class definition ANYWHERE regardless of which object it belongs.
eg.

this will print swapped values as we exchanged the values of c1 and c2 objects.
here we access c2.x at line 1 but still it works! why? because the access of var x is not related to the object of class C (that is c2 here) but its LEXICAL. we are referring to var x in the textual scope of class C. thats why it works.
in the S.o.p in main method i have used c1.x and c2.getX() just to show that we can access x in anyway.
if we had another class called A and private var y in that class which we wanted to use in class C's method test() then it wouldn't work.
hih
maulin


1. Have fun @ http://faq.javaranch.com/java/JavaRaq
2. Looking for simple infix2postfix conversion and postfix evaluation package? Click here
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Explain this!!
 
Similar Threads
Object Vs Instance
a question about polymorphism ??
Casting Array to Object
What is meant by "Object... k"