my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Accessing private variables directly from outside the class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing private variables directly from outside the class" Watch "Accessing private variables directly from outside the class" New topic
Author

Accessing private variables directly from outside the class

sandeeep shinde
Greenhorn

Joined: Aug 26, 2009
Posts: 13
Hi,
Please have a look at the code below.



This code compiles and runs. How am i able to access the name variable directly even if it is a private variable.
e.g the code p.name = "andy" should throw compilation error. Please giude me.
Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213
you need a setter method



instead of p.name = "andy";
use p.setName("andy");


SCJP 6, SCWCD 5, SCBCD 5
Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
sandeeep shinde wrote: How am i able to access the name variable directly even if it is a private variable.


You're in the same class, it doesn't matter if it's a different instance!


OCJP 6 (93%)
Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213
You're in the same class, it doesn't matter if it's a different instance!


Yes, you are correct.

Maybe what sandeeep shinde need is to "access directly from outside the class" as stated in the subject title, so my answer in the post above is to use a setter.
sandeeep shinde
Greenhorn

Joined: Aug 26, 2009
Posts: 13
Thanks a lot for your replies guys.
So it means that the private variables just cannot be accessed from other classes.
If the instance is created in the same class itself the private variable of that instance can be accessed directly.
Please correct me if I got it wrong.
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

Frankly speaking, you can access private variables from outside a class via reflection, but you shouldn't do that unless you have a very good reason to do so.


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
sandeeep shinde wrote:Thanks a lot for your replies guys.
So it means that the private variables just cannot be accessed from other classes.
If the instance is created in the same class itself the private variable of that instance can be accessed directly.
Please correct me if I got it wrong.


The instance doesn't even have to be created within the class itself. It can be passed in from the ouside (like in your example as a parameter to equals).

The reason for this behavior is quite obvious if you think about it. Otherwise many (or even most) classes would have to expose preferrably private variables and methods publicly in order to be implemented. This would be a serious blow to the concepts of encapsulation and information hiding in Java.
Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213
Freddy Wong wrote:Frankly speaking, you can access private variables from outside a class via reflection, but you shouldn't do that unless you have a very good reason to do so.


Freddy Wong,

may I know the meaning of reflection ?
Are you trying to say using an inner class with inheritance to expose the private member of enclosing class ?

Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
Lee Kian Giap wrote:
Are you trying to say using an inner class with inheritance to expose the private member of enclosing class ?


Nice getter method!

He's talking about Class.getField().
sandeeep shinde
Greenhorn

Joined: Aug 26, 2009
Posts: 13
uj nossnahoj wrote:

The reason for this behavior is quite obvious if you think about it. Otherwise many (or even most) classes would have to expose preferrably private variables and methods publicly in order to be implemented. This would be a serious blow to the concepts of encapsulation and information hiding in Java.


Does this mean in general that there is no restriction on accessing data between instances of the same class?
Still I don't get the point. Could you please elaborate on this?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38483
    
  23
No, there is no restriction on access to private members between different instances of the same class. You know about it. You wrote the class knowing where there is access to private members of other instances.
sandeeep shinde
Greenhorn

Joined: Aug 26, 2009
Posts: 13
Campbell Ritchie wrote:No, there is no restriction on access to private members between different instances of the same class. You know about it. You wrote the class knowing where there is access to private members of other instances.


No I didn't know about it. I have taken this snippet from somewhere else and was not able to understand it.
Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213
sandeeep shinde,

maybe I give you some idea, when come to access control (public, protected, "default/package", private) , try to think in the way of class, not in object. Because the purpose of access control is to decide whether you are allowed to access it in a particular class (e.g. the class itself where the member defined, or other class in the same package, or the subclass which inherit that class, or other class in different package)

In this way, you probably will not mess up the concept of access control, by mixing it with the place where you create the object and etcetera.
nick kaushik
Ranch Hand

Joined: Sep 25, 2009
Posts: 48

i dont understand the hoopla about this program...well you have an object of class so you can access its fields....you have an object of the class person & you are using it to access the fields of person class, isn't it simple? or am i missing something..?


"ye shall know the truth & the truth shall set you free..."
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
sandeeep shinde wrote:
uj nossnahoj wrote:

The reason for this behavior is quite obvious if you think about it. Otherwise many (or even most) classes would have to expose preferrably private variables and methods publicly in order to be implemented. This would be a serious blow to the concepts of encapsulation and information hiding in Java.


Does this mean in general that there is no restriction on accessing data between instances of the same class?
Still I don't get the point. Could you please elaborate on this?


That's right, there's no restriction, and my point is that this is necessary. Otherwise many classes would be forced to expose implementation detail to the outside (by raising the access rights of variables and methods that better would stay private). And this would be contrary to encapsulation and information hiding.
Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213
sandeeep shinde, to make you understand better, I have try to change the code a little bit, hope this will help

sandeeep shinde
Greenhorn

Joined: Aug 26, 2009
Posts: 13
uj nossnahoj wrote:

That's right, there's no restriction, and my point is that this is necessary. Otherwise many classes would be forced to expose implementation detail to the outside (by raising the access rights of variables and methods that better would stay private). And this would be contrary to encapsulation and information hiding.


That is the point. Why would classes be forced to expose implementation detail if we put the restriction?
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
sandeeep shinde wrote:
That is the point. Why would classes be forced to expose implementation detail if we put the restriction?


Well, I'm no longer sure we're talking about the same thing, but consider this,



This code works because we can access private members of the Person object p passed into the setSameName method.

Now if this wasn't possible (because passed in Person objects were treated like any other passed in object) we would be forced to do this,



As you can see the name variable is no longer a private implementation detail, it's public. It's exposed to the outside. Anybody could fiddle with it.

So things are the way they are because otherwise we would be forced to expose implementation detail to the outside. And this would severely compromise encapsulation and information hiding in Java.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38483
    
  23
I am quite convinced there are lots of people who read "private means access within the same class" and think it means "private means access within the same object". It doesn't. As uj has pointed out, private access within the same class, not the same object, is necessary.

Try writing an equals() method without access to the private fields of the "other" object . . .
sandeeep shinde
Greenhorn

Joined: Aug 26, 2009
Posts: 13
Thanks for your replies guys. I got the point.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38483
    
  23
Well done
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing private variables directly from outside the class