• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

confuse about protected and static

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

and when the code modifyied as following:

why add the static modifier the code can complile correct?
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main() method is static and cannot access a nontstatic variable.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks very much! and Where to download the Java Programming language 3rd Edititon?
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}
}
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic