-2

Edited to fix being marked as duplicate or to be more clear on why it appears to be a duplicate. At the time I did not know that package and default where the same thus reason for this post.


now I going through exam questions in preparation for my Java exam and I have a question asking me to explain access modifiers and its asking me about a Package modifier.

I can find info on private, Protected, public and default but can't find anything on Package.

can someone please give me an answer or link me to an article about it?

2 Answers2

7

package-private is not a real modifier. You can't type package-private and get the system to recognize it as an access modifier. It's really the default, made by not including any other modifiers.

It means that the given members can only be accessed in the same package.

For example, com.hexafraction.Cow can access a member with default modifiers(none actually) in com.hexafraction.Dog, but com.foo.Crow can't access that member as it's not in the same pacakge.

In this example, the following makes up Cow:

pacakge com.hexafraction;
class Cow{
    void moo(){ //no public, protected, or private modifier
        System.out.println("moo!");
    }
}

Edit for the future: In Java 8, package will supposedly be the modifier needed for this. Literally typing default will still not apply here.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • 2
    An example of a package private member would help to clarify your answer. – Jeffrey Jul 29 '13 at 17:01
  • @Jeffrey This edit OK? – nanofarad Jul 29 '13 at 17:04
  • An interesting tidbit: as of Java 8, `package` will be a legal access modifier. `default`, on the other hand, will also be a legal modifier, but not for access level. – Marko Topolnik Jul 29 '13 at 17:09
  • 1
    @MarkoTopolnik Link to documentation stating this? And what will not having a modifier do? – nanofarad Jul 29 '13 at 17:10
  • JSR 335: Lambda Expressions. `package` will be an optional modifier, for clarity. I don't know if you have been aware of this, but the official name for package-private access level has so far been "default" access level. The new use of `default` causes confusion, therefore the introduction of `package` modifier, and the rename of the access level to "package-private". – Marko Topolnik Jul 29 '13 at 17:11
  • so its very similar to the default? – user2631161 Jul 29 '13 at 17:12
  • @user2631161 Package-private is the default that is used when no modifier is given. – nanofarad Jul 29 '13 at 17:12
  • Thanks just what I needed to know. Have a good day ^_^ – user2631161 Jul 29 '13 at 17:14
  • @MarkoTopolnik The newest [revision of JSR 335](http://cr.openjdk.java.net/~dlsmith/jsr335-0.6.2.html) claims `Removed support for an explicit package access modifier. ` – Jeffrey Jul 29 '13 at 17:15
  • @Jeffrey Interesting, this version has not yet been officially published. – Marko Topolnik Jul 29 '13 at 17:17
3

The so-called "package-private" access level is what occurs without a modifier such as private, protected, or public.

Example:

public class Test {
    int test;  // package-private
}

Anything in the same package, even an unrelated class, can access it, but other classes (even subclasses of the class) outside of the package cannot access it.

This link to the Java tutorial on the subject should help.

rgettman
  • 176,041
  • 30
  • 275
  • 357