It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confuse about protected and static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confuse about protected and static" Watch "confuse about protected and static" New topic
Author

confuse about protected and static

dx wu
Ranch Hand

Joined: Apr 28, 2002
Posts: 60

and when the code modifyied as following:

why add the static modifier the code can complile correct?


good good study , day day up!
Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 107
The main() method is static and cannot access a nontstatic variable.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"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


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Mar 13, 2004
Posts: 1272
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.


Mike Gershman
SCJP 1.4, SCWCD in process
dx wu
Ranch Hand

Joined: Apr 28, 2002
Posts: 60
thanks very much! and Where to download the Java Programming language 3rd Edititon?
navneet shrivastava
Ranch Hand

Joined: Jul 09, 2004
Posts: 39
oOPS! i dont agree on this ..or may be its incomplete :

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


+nav
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Jul 09, 2004
Posts: 39
--------------------

"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

Joined: Sep 22, 2004
Posts: 91
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
}
}


SCJP 1.4 <br />SCWCD 1.4 (preparing)<br />SCBCD 1.3 (coming soon)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confuse about protected and static
 
Similar Threads
Can a class extend a class from diff. package?????
Query on Q49, RHE Bonus 1 - final 'default' method overriding
protected method not being overridden in diff. package
Output of the Program
Access to protected member features