File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Protected - access modifier 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 "Protected - access modifier" Watch "Protected - access modifier" New topic

Protected - access modifier

Kamil Wojcik
Ranch Hand

Joined: May 19, 2008
Posts: 78

I have one problem in understand one thing about protected modifier, here examples:

When I add static to variable x in the first class everything is fine, why? I don't get it.
Thanks for help!

Source: SCJP K. Sierra, B. Bates

SCJP 6.0 Passed | SCWCD Passed | PSM in progress...
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
That is because If you mark it static then you dont need a reference to access it.
anand-k jha

Joined: Dec 02, 2008
Posts: 15
A protected member can not access through instance of superclass, which is in another package.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
When you do pp.x and x is a static member of pp's class (Parent) this is interpreted by the compiler as Parent.x. Notice that you still have the access control restrictions imposed by the protected access modifier. That means that when a member is both static and protected, it can be accessed by any class in the same package as the class where the member is declared, and in any subclass of the class where the member is declared. Like Abhi said, in this case you don't need an instance of the superclass to access the member, because the member is static (remember that pp.x is just notation for Parent.x.)

All code in my posts, unless a source is explicitly mentioned, is my own.
I agree. Here's the link:
subject: Protected - access modifier
It's not a secret anymore!