aspose file tools*
The moose likes Beginning Java and the fly likes Why this private member can be seen? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why this private member can be seen?" Watch "Why this private member can be seen?" New topic
Author

Why this private member can be seen?

Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 433


Why a.num can be seen here? What does the private mean?


To be or not to be. It's a question.
Keith Pitty
Ranch Hand

Joined: Sep 22, 2005
Posts: 32
The keyword private indicates that a member is private to that class.

So in the contrived example where you have a constructor of the form public MyClass(MyClass myObj), it is indeed valid to refer to private members of myObj directly.

Why? Because the object reference being passed in is of the type that is being constructed. The code is all there in the class.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
With other words, private doesn't restrict access to the object, but to the class.

This makes sense when you think about what access modifiers are good for: managing code dependencies.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 433

private doesn't restrict access to the object, but to the class.

The a.num can be seen just because a appears in class A.
a is an object of class A, private members can be seen in the class, so a.num can be seen in A. Can I catch you meaning?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Yes, I think you got it!
Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 433
Thanks for your answer.
Another question come up:
Why this constructor? It has some special useage?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why this private member can be seen?