File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Private means Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Private means" Watch "Private means" New topic
Author

Private means

Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Can one object acess a private variable of another object of the same class ?

Ans: Yes

Could any one explaine this rule with one example?


Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Here is an example



and



When run, the following results occur



Hope that helps.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

"shree shree"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608


Tony Morris
Java Q&A (FAQ, Trivia)
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi Mark,
whots wrong in my name ?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

You need to have your real first and real last names, not one or the other repeated.

Funny, you fixed it as I posted this. Your name now complies. Thanks.

Mark
[ April 06, 2005: Message edited by: Mark Spritzler ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Mark

I am not able to understand this concept. The code belows uses two different classes and objects of these classes to access a private variable.



Forgot to attach the output.

getanotherName: Hello
a.getName: Hello
b.getName: null
[ April 06, 2005: Message edited by: Anupam Sinha ]
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Anupam,
See variables are never overridden, they are hidden. This means that if the variables are called under class A's scope, variable of class A will be visible. While if its under class B's scope, variable of class B will be visible.
So here since "name" variable is initialized to "World" in class B but this is hidden in class A, in A the same variable is null. Class A cannot see class B's varaible. So it displays null.
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi Animesh
1. Class B has getName() method. Bcoz it is inherited from A.
2. Now class B object called the getName() method: b.getName().
3. B has own memeber variable b and initialized as world
4. Why it is print null.

So here since "name" variable is initialized to "World" in class B but this is hidden in class A, in A the same variable is null.


But the below code is work fine.


Output:
A value From A Scope 100
B value From B Scope 200
A value From B scope 100

How is it possible.
I am Confused. Is anything I understand wrong?
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
So whats the problem u r having, i guess its in this statement
System.out.println("A Value From B Scope " +b.getA());

right?
Ok whats happening here is that since methods are inherited from super class to sub class, so u can very well access the method getA(). Now since this method accesses the variable a, which is initialized to 100, u get the output. Now one more thing, see when u create an instance of a class, beore the instance is created constructor of super class is also run and in effect all its instance initializers. So lets see whats happening here

When u create an object of A, u also initialize the variable a to 100;
When u create an object of B, u initailize A's instance members( A is super class of B, here a is initialized to 100) as well as instance members of class B
U getting it?
So when getA() is called from Class A, u get the variable 100 printed for a.

Now lets take up the previous example for u.
In that example :

See above, i have modified the code for u.
When u create an instance of Class A, u get name initialized for the class.
When u create an instance of Class B, the no-arg constructor of class A is also run because by default JVM inserts the statement "super()". In ur earlier case, since A's default constructor was being run, u got the value as null for the string name.
But now look at the code i have given, here in the no-arg constructor of A, i am also initializing the variable name of A.
Run the program and tell me if u have any problem in justifying the output.

Thanks
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi Animesh
Thanks lot. Now I am cleared in Inheritance with member hiding.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Animesh

Thanks for the reply. I wanted to know that even when a (Object ref. to A) and b (Object ref to B) belong to two different classes then also



the above line executes.

In the code above an Object of type B is changing an Object of Type A.

So does it mean "Can one object acess a private variable of another object of the same class and a diferent class aswell.
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298

So does it mean "Can one object acess a private variable of another object of the same class and a diferent class aswell.

No an object cannot directly access the private variable of another object of the same class. It can only be done using a public method which accesses the variable , like its done using anotherName() method
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Animesh]: No an object cannot directly access the private variable of another object of the same class.

I guess that means you haven't actually tried it, eh?


"I'm not back." - Bill Harding, Twister
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi jim,
Is it possible? how ?
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Animesh: No an object cannot directly access the private variable of another object of the same class.

The example given by Mark has demonstrated that it's possible.

Extracted from the second post of this thread.

[ April 07, 2005: Message edited by: Joyce Lee ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Animesh and Raghu gave really good explanations there.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Private means