Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access Specifier

 
Udegbunam Morah
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • 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.
 
vinuharan haran
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
replace obj.x with x in println statement.
The code will compile fine.
 
Rahul Bhosale
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
oops..sorry
 
vinuharan haran
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • 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...
 
M Rama
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic