• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Access Specifier

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the code listing below:

And this:


On compilation, I got the following result:


I will appreciate any suggestion please. Thanks.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
replace obj.x with x in println statement.
The code will compile fine.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
vinuharan haran
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops..sorry
 
vinuharan haran
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic