aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Protected - access modifier" Watch "Protected - access modifier" New topic
Author

Protected - access modifier

Kamil Wojcik
Ranch Hand

Joined: May 19, 2008
Posts: 78

Hi,
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
Greenhorn

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Protected - access modifier
 
Similar Threads
doubt in access modifier. Please explain this code
Problem about protected attribute
Access protected member from the class of different package.
protected modifier
Child extends Parent, Belongs to same package, Cannot access public variable