File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes access modifiers and inheritance 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 "access modifiers and inheritance" Watch "access modifiers and inheritance" New topic
Author

access modifiers and inheritance

See Furst
Greenhorn

Joined: Aug 04, 2010
Posts: 29
Ok so I wrote this code below to look at statics, inheritance and access modifiers and once again Java totally has me scratching my head..

We are told that variables without access modifiers default to access from only within the package.. this is why you have protected variables, so that subclasses is other packages can access those variables.

So check this out.. this completely baffles me.

in one file I have the following:



In another file I have the following.. This is located in the com\thing\mine subfolder and classpath defaults to \.\ (Yep, Windows XP)



Why can I access thing from a different package, different class when it's static??? Why can I access it from a different package when it's access is package only???

This code compiles, runs and the output is:

thing: 3
setthing to 5? 5





Doin' Java to be one of the cool kids.
I usually use Perl;
Gari Jain
Ranch Hand

Joined: Jun 29, 2009
Posts: 100
com/thing/mine/CanIgetThing.java:2: '.' expected
import StaticInheritance;
---------------------------^


This error I am getting while trying to compile your code.


OCPJP 6-100%; Preparing for GATE11
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

A class in a package, cannot access a class in the default package...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Anton Brass
Greenhorn

Joined: Sep 08, 2010
Posts: 25
yep, possibly your IDE helped you somehow out. But if you use plain old javac this code won't compile since you cannot accesss classes out of a package that are in the default one.
See Furst
Greenhorn

Joined: Aug 04, 2010
Posts: 29
Anton Brass wrote:yep, possibly your IDE helped you somehow out. But if you use plain old javac this code won't compile since you cannot accesss classes out of a package that are in the default one.


No and no... but it was a serious gotcha...

There was a an older class file in the classpath from a previous revision...

Once that class file was deleted...

compilation failed.

Thanks for the help.

I thought something was bass ackwards.
See Furst
Greenhorn

Joined: Aug 04, 2010
Posts: 29
See Furst wrote:
Anton Brass wrote:yep, possibly your IDE helped you somehow out. But if you use plain old javac this code won't compile since you cannot accesss classes out of a package that are in the default one.


No and no... but it was a serious gotcha...

There was a an older class file in the classpath from a previous revision...

Once that class file was deleted...

compilation failed.

Thanks for the help.

I thought something was bass ackwards.


What I wanted to prove was that this code would not compile because the default access modifier was on the package level:



and in CanIgetThing.java



This fails with the expected "thing is not public error"

however it works when thing is protected:



thing: 3
setthing to 5? 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: access modifiers and inheritance