jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static variables and protected access in a different package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static variables and protected access in a different package" Watch "Static variables and protected access in a different package" New topic
Author

Static variables and protected access in a different package

Simran Dass
Ranch Hand

Joined: Jan 09, 2010
Posts: 183


Its not possible to access a protected instance variable in another package using the upreclass object. For e.g in the foll code line 3 gives Compiler ERROR. But why do not the lines 2 and 3 give any compiler error


Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Because "y" has "default" access specifier, and default can be seen in the package only. Its package only access specifier.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Simran Dass wrote:Its not possible to access a protected instance variable in another package using the upreclass object. But why do not the lines 2 and 3 give any compiler error[/color]

First, protected can be accessed into the sub class having different package and what you've illustrated with the example is the access of "static" (class) variable and not its protected behavior.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
protected members when used by subclasses in different packages, should be accessed through inheritence only. there is no other way to access protected members in different package.
that's why line 3 gives error.


But why do not the lines 2 and 3 give any compiler error


I guess you meant line 1 and 2.

Here you are accessing a static member which can be accessed by either class name or class reference irrespective of package provided class is public.


SCJP 1.6 96%
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

I think, Your question is wrong.......

The line 3 is only making trouble. Because, It's accessing the protected member of Class of different package. For a subclass outside the package, the protected member can be accessed only through inheritance.

In line 1, The compiler will changed that line after compiling to as line 2. And here static is dominating.......


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
 
 
subject: Static variables and protected access in a different package
 
Similar Threads
a protected question
Development: trouble with import and classpath
inner classes
accessing the super class variable
about protected static members