Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confuse about protected and static

 
dx wu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

and when the code modifyied as following:

why add the static modifier the code can complile correct?
 
Richard Vagner
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main() method is static and cannot access a nontstatic variable.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"wolfoxer java" you habe been asked before to change your displayed name to conform to our JavaRanch Naming Policy.

I request you one last time to change it.
Thanks
-Barry
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your second example (with static) is really equivalent to the following:


Access is now allowed because class B extends A and i is protected.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From The Java Programming Language, 3rd Edition:
protected members can also be accessed from a class through object references that are at least the same type as the class - that is, references of the class's type or one of its subtypes

However:
Protected static members can be accessed in any subclass


So you can fix the original code either by making "i" static or by changing "aa" to a B reference as follows: "B aa = new B();"

This is really obscure stuff, but fair game for the exam.
 
dx wu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks very much! and Where to download the Java Programming language 3rd Edititon?
 
navneet shrivastava
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oOPS! i dont agree on this ..or may be its incomplete :

"Access is now allowed because class B extends A and i is protected."
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by navneet shrivastava:
oOPS! i dont agree on this ..or may be its incomplete :

"Access is now allowed because class B extends A and i is protected."



In what way do you disagree?
 
navneet shrivastava
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
--------------------

"Access is now allowed because class B extends A and i is protected."

In what way do you disagree?
--------------------

By the defination of "protected" , a protected memeber is accessible to a subclass in the same/different package.

please correct me if u think am wrong
 
Francis Palattao
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see protected within the same package as same behaviour as default access.


However, no access when outside the package with the exception if you subclass a class that has protected members. Moreover, you have access to the protected members when you create an object of the class you used to subclass.

package com.A;
class A{
//protected members here
}
class C{
//protected members here

}

package com.B;
Class B extends A{

public static void main (String []args){

B b = new B();

// b has access to protected members

C c = new C();

// c has no access to protected members
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic