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
97
votes
7 answers

Isn't "package private" member access synonymous with the default (no-modifier) access?

I am a little confused over the term "package private" that some of the documentation uses, along with the usage of "default access." Aren't package-private and default access both synonymous with protected?
TurtleToes
  • 2,047
  • 2
  • 17
  • 17
75
votes
8 answers

Pros and cons of package private classes in Java?

I am learning Java recently, and I came across the notion of package-private classes, which is the default if we don't specify anything. But then I realized: I seldom see the use of package-private class. Is there a reason for this, e.g., it has…
zw324
  • 26,764
  • 16
  • 85
  • 118
68
votes
2 answers

Why can a enum have a package-private constructor?

Since an enum constructor can only be invoked by its constants, why is it then allowed to be package-private?
Tobias
  • 1,062
  • 1
  • 9
  • 15
64
votes
5 answers

Accessing non-visible classes with reflection

I am trying to get an instance of a non-visible class, AKA package private class, using reflection. I was wondering if there was a way to switch the modifiers to make it public and then access it using Class.forName. When I try that now it stops…
Josh Sobel
  • 1,268
  • 3
  • 14
  • 27
38
votes
2 answers

Why JUnit 5 default access modifier changed to package-private

Why is the default access modifier in JUnit 5 package-private? Tests in JUnit 4 had to be public. What is the benefit of changing it to package-private?
wojtek1902
  • 503
  • 2
  • 10
  • 25
37
votes
3 answers

Make an internal function visible for unit tests

In case the tests are in a different module than the production code (which is common), what's the best way to make internal functions visible for tests? In Java, I would have the production code and the test in the same package and make the…
Jan Pomikálek
  • 1,369
  • 2
  • 13
  • 22
22
votes
4 answers

Inheritance at package visibility in Java

I am looking for an explanation for the following behavior: I have 6 classes, {a.A,b.B,c.C,a.D,b.E,c.F}, each having a package visible m() method that writes out the class name. I have an a.Main class with a main method that does some testing of…
TFuto
  • 1,361
  • 15
  • 33
22
votes
1 answer

Lambda expression fails with a java.lang.BootstrapMethodError at runtime

In one package (a) I have two functional interfaces: package a; @FunctionalInterface interface Applicable> { void apply(A self); } - package a; @FunctionalInterface public interface SomeApplicable extends…
Bubletan
  • 3,833
  • 6
  • 25
  • 33
17
votes
2 answers

Why doesn't C# have package private?

I'm learning C# and coming from a Java world, I was a little confused to see that C# doesn't have a "package private". Most comments I've seen regarding this amount to "You cannot do it; the language wasn't designed this way". I also saw some…
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
14
votes
1 answer

Package-private scope in Scala visible from Java

I just found out about a pretty weird behaviour of Scala scoping when bytecode generated from Scala code is used from Java code. Consider the following snippet using Spark (Spark 1.4, Hadoop 2.6): import java.util.Arrays; import…
Dici
  • 25,226
  • 7
  • 41
  • 82
13
votes
4 answers

Outside classes accessing package-private methods

Suppose I have a class in my package org.jake and it has a method with default access (no modifier). Then the method is visible inside the package only. However, when someone receives the jar of my framework, what is to stop them from writing a new…
Jake
  • 15,007
  • 22
  • 70
  • 86
9
votes
5 answers

Publicly declare a package private type in a method signature

This is possible in Java: package x; public class X { // How can this method be public?? public Y getY() { return new Y(); } } class Y {} So what's a good reason the Java compiler lets me declare the getY() method as public?…
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
9
votes
3 answers

Is there a way to execute unsafe code (disable security manager) in Java?

Please don't post an answer saying "you shouldn't do this." I don't plan to use this in production code, but only for some hacking fun. In answering this question, I wanted to run some arbitrary unsafe Java code for fun. The code in question…
Andrew Mao
  • 35,740
  • 23
  • 143
  • 224
8
votes
3 answers

Moving a package-private class—should I consider that binary incompatible?

Because of an issue with package name aux under Windows, I am moving a helper class within the package hierarchy of my library from de.sciss.scalainterpreter.aux to de.sciss.scalainterpreter The class is private to the library, i.e.…
0__
  • 66,707
  • 21
  • 171
  • 266
7
votes
2 answers

How can I open class only to test class?

I'm mainly a Java developer and wonder about structure when writing unit test in kotlin, Assuming there's no package-private in kotlin private to restrict visibility to the file internal to restrict visibility to the module How can I open class…
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
1
2 3 4