• 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

Accessing private variables directly from outside the class

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you need a setter method



instead of p.name = "andy";
use p.setName("andy");
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 48
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..?
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sandeeep shinde, to make you understand better, I have try to change the code a little bit, hope this will help

 
sandeeep shinde
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies guys. I got the point.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic