Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

access modifiers and inheritance

 
See Furst
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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




 
Gari Jain
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
com/thing/mine/CanIgetThing.java:2: '.' expected
import StaticInheritance;
---------------------------^


This error I am getting while trying to compile your code.
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class in a package, cannot access a class in the default package...
 
Anton Brass
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic