• Post Reply Bookmark Topic Watch Topic
  • New Topic

Access modifiers and packages?  RSS feed

 
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I'm reading if you don't specify an access modifier the default is package private.

when I declared a method private int getBottles() and try to access it via  main class I receive  getBottles() has private access in BottlesOfBeer. Which is fine.

Now when I don't specify a modifier, such as int getBottles(), I do not receive the access error. System.out.println(newBeerCount.getBottles()); and it prints out no errors

Does the default package private only for declaring Class modifiers. Everything I have read thus far states that it is the default for classes, fields and methods.

Ok as far as packages go. Both BottlesOfBeer.class and StartBeerSong.class reside in the same folder which I created. Have not declared either class as being part of a package.

Does java treat a folder whether declared a package as a package? So being that my folder Java contains both these classes, it is not generating an access error because these classes are in the same folder.



 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think yes to all your questions.
If you do not declare a package name, the folder your class is in is called the “unnamed package”, though you will hear people call it default package or similar.
The official name for access where no access modifier is given is default access, but many people, myself included, call it package‑private.
Access modifiers apply to classes (including nested types), fields, constructors, and methods; any of those not declared private is accessible from any Java® code in the same package, which, if you don't use package names, means all your code.

The rules are slightly different for nested types; the scope of private access is the enclosing type.
 
Chad McAte
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok not sure but doesn't seem like int getBottles() is defaulting to package-private. Was trying to prove it by putting class files in separate folders

C:\Programming\Java\Packages\A BottlesOfBeer.java
and
C:\Programming\Java\Packages\B StartBeerSong.java

then

Programming\Java\Packages\A javac -cp . BottlesOfBeer.java
C:\Programming\Java\Packages\B>javac -cp C:\Programming\Java\Packages\A StartBeerSong.java
java -cp C:\Programming\Java\Packages\A;C:\Programming\Java\Packages\B StartBeerSong

And the program compiles without an access error. Is there something else Im missing about access modifiers?



 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your classpath seems wrong. It is supposed to specify the root directory, and not specify the parts that are part of the package.

Chad McAte wrote:Ok not sure but doesn't seem like int getBottles() is defaulting to package-private. Was trying to prove it by putting class files in separate folders


Also, you did specify the different packages right?

Henry
 
Chad McAte
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so neither class was defined with a package statement. Ok so default access modifier only pertains to packages, I proved that using eclipse. After creating my project and to seperate packages the IDE was stating getBottles() is not visable.

So why am I able to access int getBottles() when a package is not declared. I know its not proper if this was a stand alone class not to specify public or private just trying to understand why this happening.



 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chad McAte wrote:
So why am I able to access int getBottles() when a package is not declared. I know its not proper if this was a stand alone class not to specify public or private just trying to understand why this happening.


If neither class defines a package statement, then both classes are in the same unnamed package. Placing the classes into separate directories, and then combining those directories together via a classpath, doesn't change that -- the classes are both in the same package.

Henry
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if you don't combine the two packages with the CLASSPATH, they are independent and ne'er the twain shall meet. You cannot even use imports to gain access from one directory to the other.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!