Questions tagged [package-private]

Package private is the default access-control modifier in Java. If a member of a class is not annotated with `private`, `protected`, or `public`, then the member is `package private` by default. When a member is `package-private`, it can only be accessed by the parent class, and other classes in the same package.

Consider the following classes:

package my.stuff;

class Foo {
  private int a;
  int b;
}

package my.stuff;

public class Bar {
  private int c;
  private int d;
}

package my.otherstuff;

public class Baz {
  private int c;
  private int d;
}

Class Foo is accessible from class Bar, but not from class Baz. This is because both Foo and Bar are in the same package (my.stuff). Also, the field Foo.b is accessible from Bar, but not from Baz for the same reasons.

48 questions
7
votes
3 answers

Enum implementing interface, interface and method visibility

I just came accross the following code, which surprised me a little bit, I converted it to a simple SSCEE here though: custompackage.package1.MyEnum.java public enum MyEnum implements MyInterface { CONSTANT_ONE() { @Override …
skiwi
  • 66,971
  • 31
  • 131
  • 216
6
votes
2 answers

When would I use package-private in Java?

I love access control in any language, but I find that in Java I almost never (if ever) use the package-private access modifier (or lack thereof). I realize that inner classes can be private, protected, or package-private, but outer classes can only…
NobleUplift
  • 5,631
  • 8
  • 45
  • 87
6
votes
2 answers

How to share package private data between two packages in Java?

I have 2 Java packages, A & B. Let's say that some classes in package B want to use some classes in package A however, when a developer comes along and develops package C (or, say, application C), he/she will use my package B but I do not want…
fatfreddyscat
  • 835
  • 3
  • 8
  • 26
5
votes
1 answer

Why is the EJB bean method not detected if it is package private?

This one is puzzling me. I created a minimal JAX-RS application based on JavaEE7 running on Wildfly 10.1. @ApplicationPath("") public class JAXRSConfiguration extends Application { @Override public Set> getClasses() { …
Mark
  • 2,167
  • 4
  • 32
  • 64
5
votes
2 answers

Is there a way to use package-private in java to allow other packages access?

So I've been working in C# for a number of years and am now working in Java. In C#, you can use the internal keyword to hide a class/method from public view but allow certain assemblies/packages access if you specifically grant it. As I look…
Pinski
  • 2,607
  • 2
  • 24
  • 25
5
votes
2 answers

accessing package-private fields in classes shared across Eclipse projects

I have a model class (MVC pattern) that I'm using in two Eclipse projects. One project, let's call it Producer, is capturing data from a stream and storing it to a database. The model class in question, say ObjectModel, is used to deserialize the…
ericsoco
  • 24,913
  • 29
  • 97
  • 127
4
votes
2 answers

Javascript: Export things inside the module, internally

In Java, we have 4 visibility levels. Except public and private, we have protected level and a default level (with no modifier) that is also called "package-local" or…
Mir-Ismaili
  • 13,974
  • 8
  • 82
  • 100
4
votes
2 answers

How does a java compiler resolve a non-imported name

Consider I use a type X in my java compilation unit from package foo.bar and X is not defined in the compilation unit itself nor is it directly imported. How does a java compiler resolve X now efficiently? There are a few possibilities where X could…
gexicide
  • 38,535
  • 21
  • 92
  • 152
3
votes
1 answer

howt to use mBagOfTags in ViewModel android?

I have object that implement Closeable interface and want to close() method called when my ViewModel is clear. I know to implement onCleared method of ViewModel but I want to use mBagOfTags in ViewModel. mBagOfTags handle closeable object (call…
Ali Elahi
  • 131
  • 1
  • 10
3
votes
1 answer

Lombok builder package scope

I would like to generate package-scope builder using Lombok, but I'm not sure if it's possible (I didn't find any clues in documentation). By default Lombok generates public builder, i.e. this code: @Builder class User { private final String…
k13i
  • 4,011
  • 3
  • 35
  • 63
3
votes
1 answer

Internal Package Protected access modifier in Java

As far as I know, it is impossible classical way to add for a class member in Java internal package protected access modifier (such as internal protected in C#) when its class has public access modifier. Do you know any way or design pattern to do…
pt12lol
  • 2,332
  • 1
  • 22
  • 48
2
votes
1 answer

Are packages (that share a name) combined during compliation?

I'm almost certain this question has been asked, but I'm not sure what to search for regarding it. Anyway, I was was curious if it would be possible to create a class that extends ByteBuffer. I thought it would be impossible due to ByteBuffer…
Jacob G.
  • 28,856
  • 5
  • 62
  • 116
2
votes
2 answers

Why can't I access package-private fields in the android.widget package?

I'm attempting to override an Android View class to tweak the functionality just slightly. But I need to modify a field that does not have a setter method. I've placed the subclass in a package called android.widget. Why can't I access any of the…
Neil Traft
  • 18,367
  • 15
  • 63
  • 70
2
votes
1 answer

javadoc and package private interface?

This maybe the same issue with How can I prevent a package-private interface appearing in Javadoc?. But I thing the situation is a little bit different. interface Child

{ // package-private!!! internal-use only!!! P getParent(); void…

Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
2
votes
2 answers

Java - Difference between private and package-private enum constructor

Recently I'm quite oftenly using Enumerations. So I wonder... Is there any difference between a private Enum constructor and a enum constructor withour any visibility modifier (package-private)?
MinecraftShamrock
  • 3,504
  • 2
  • 25
  • 44