This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Why is a private member of a class accessible using a class object in main()

 
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why I am able to access a private member of a class using an instance of the same class? Doesn't that beat the purpose of data hiding?
I came across this example while reading through book "OCA Study Guide by Jeanne Boyarsky and Scott Selikoff". This example is given under Chapter 1 - Java Building Blocks, topic "Order of Initialization".
 
Sheriff
Posts: 24654
58
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not at all. "private" means "access restricted to this class only". Not "access restricted to this object only".

If you think about it, you'll see that the compiler can't possibly implement the latter rule. When an object of class Chick accesses a private member of class Chick, there's no way for it to tell whether that member belongs to that object or to some other object of class Chick. However the former rule, the one which actually applies, it's possible for the compiler to implement that one.

In the example you posted, it's code in the Chick class which is accessing the "name" member of some Chick object -- and it doesn't matter which Chick object. Even code in static methods of Chick can do that, as the example shows.
 
Paul Clapham
Sheriff
Posts: 24654
58
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, welcome to the Ranch!
 
Siddhant Agarwal
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I understand now. I went through various posts on stack overflow as well discussing the same thing and then tried with a sample code where the object of Chick class was created in some other class. So as long as the object uses the private member within the class, it works. If I create the Chick object in some other class, and try to access the private member of Chick, it shows me an error.
Thanks!
 
Paul Clapham
Sheriff
Posts: 24654
58
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Writing sample code to investigate language features, that's a good strategy. Glad it worked out for you!
 
I child proofed my house but they still get in. Distract them with this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!