File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Access Specifier 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 "Access Specifier" Watch "Access Specifier" New topic
Author

Access Specifier

Udegbunam Morah
Ranch Hand

Joined: Oct 13, 2002
Posts: 36
Look at the code listing below:

And this:


On compilation, I got the following result:


I will appreciate any suggestion please. Thanks.


Udegbunam Ikechukwu Morah<br />B.Sc.,SCJP, MCP, OCP, PMP<br />Manager, Business COnsulting Group<br />Deep Business Solutions Limited
vinuharan haran
Ranch Hand

Joined: Feb 26, 2005
Posts: 64
replace obj.x with x in println statement.
The code will compile fine.
Rahul Bhosale
Ranch Hand

Joined: Mar 10, 2005
Posts: 77

How can you access non-static members from a static method?

I guess the anwer to that question is, the member of class Class2 is being accessed in violation of protected access.
If you did this it will compile and run fine.



RB
vinuharan haran
Ranch Hand

Joined: Feb 26, 2005
Posts: 64
oops..sorry
vinuharan haran
Ranch Hand

Joined: Feb 26, 2005
Posts: 64
what i meant to say is...inorder to access a variable declared protected in superclass(say in a package different from the one in which sub class is defined), it can be done thru its simple name(provided there is no hiding) or thru its subclassinstance.variable.
Definitely the simple name cannot be used in static context.
once again i apologise...
M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
K&B says the following:

For subclasses outside the package, the protected member can be accessed
only through inheritance; a subclass outside the package cannot access a
protected member by using a reference to an instance of the superclass (in
other words, inheritance is the only mechanism for a subclass outside the
package to access a protected member of its superclass).
Udegbunam Morah
Ranch Hand

Joined: Oct 13, 2002
Posts: 36
Originally posted by M Rama:
K&B says the following:

For subclasses outside the package, the protected member can be accessed
only through inheritance; a subclass outside the package cannot access a
protected member by using a reference to an instance of the superclass (in
other words, inheritance is the only mechanism for a subclass outside the
package to access a protected member of its superclass).


Thanks Rama. That is exactly what I am looking for. Good research skills there. Thanks to everyone who participated in the explanation too. Thanks a lot.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Access Specifier