Pros and cons of private class packages in Java?

I have been learning Java recently, and I came across the concept of package-private classes, which is the default if we don't specify anything. But then I realized:

  • I rarely see the use of a private class package. Is there a reason for this, for example, has serious flaws, is it redundant, or is it just that I do not read enough? Are there strong arguments regarding / against its use?

  • If this is really not useful in most cases, why will it be the default?

  • In what situation should we use package-private in the real world? Ie, when will it become indispensable?

In other words, what are the main pros and cons of the default package-private modifier?

+49
java class package-private default
Jun 24. 2018-11-11T00:
source share
8 answers

The short answer is a somewhat broader form of quotient.

I assume that you are familiar with the difference between public and private and why it is generally good practice to create private methods and variables if they are used exclusively within the class in question.

Well, as a complement to this - if you are considering creating your software in a modular way, you can think of a public interface to your module, which will have several classes inside it that will work together. In this context, it makes sense to make public methods if called by consumers; private if they are internal to the class; and package private , if they are used to call between classes in this module, i.e. this is a detail of the implementation of your module (as seen by public subscribers), but it covers several classes.

This is rarely used in practice, because a batch system is not so useful for this kind of thing. You will have to dump all the classes for this module into the same package, which for some non-trivial things will be a little cumbersome. So, the idea is great - to make the method available only for a few "nearby" classes, as a slightly wider private , but the restrictions on how you define this set of classes means that it is rarely used / useful.

+41
Jun 24 '11 at 16:20
source share

One nice thing about the private package is that you can use it to provide access to methods that would otherwise be considered private for unit test classes. The disadvantage, of course, is that other classes in the package could call it when they really shouldn't.

+8
Jun 24 '11 at 17:14
source share

Regarding the question of “why would this be the default”, in this context, the term “default” means only the absence of another classifier. I think they might have come up with another keyword (the "package" was already taken), but they did not.

In the real world, I use default access for utility classes and abstract classes, which I don’t want people to call or otherwise use from other packages. Say you have an interface and two specific implementations that extend from some abstract class. You declare your two concrete classes final because you don't necessarily want people to subclass them (see Effective Java). You also don't want monkey people with your abstract class for the same reason. If you use the default access for an abstract class, then people only see it if they put their class in your package. This is not bullet proof, but I find this to be a reasonable use / illustration of default access. However, the fact that it does not interfere with the details of the leak as confidential, i.e. It does not guarantee anything, it means that this is not a particularly useful agreement.

Another reason you haven't seen it more often is because people tend to exclude classes with default access from their javadocs.

+2
Jun 24 '11 at 16:32
source share

In addition to encapsulation, one of the main advantages of using package-private classes is that they do not appear in the javadoc of your project. Therefore, if you use some helper classes that have no other use, but to help your public classes do something that clients need, it makes sense to make them private, because you want to make it as simple as possible for library users.

As an example, you can take a look at the library that I developed. javadoc contains only 5 interfaces and 12 classes, although the source code has a lot more. But what is hidden is basically inner layers that do not provide added value to the client (as a rule, all abstract base classes are hidden).

There are also many examples in the JDK.

+2
Jul 01 '13 at 20:08
source share

1 - depends on architecture. In general, if you write code only for yourself and for small projects, you probably will not use it. In larger projects, it can be helpful to ensure that you can control where and how certain methods are called.

2 - By default (Ie public / protected / private) is not the same as private - this is the 4th state. See Java Access Control

3. It can make life easier when you write libraries that don’t want third parties to rely on how you implement the base code — you just publish the API itself.

+1
Jun 24 '11 at 16:08
source share

Please note that when you talk about classes, you have only two options:

  • public classes
  • private classes

The concept of "private class" does not make sense. (Why make a class that isn't used anywhere ?!)

So, if you have a class for intermediate operations that should not be accessible to API users, you should declare it as a “closed package”

Also, when you define many classes in one source file, you are allowed to publish only one class (its name matches the name of the .java file). If any other class is defined in the same file, it must be a "private package".

0
Jan 03 '13 at 15:04
source share

Private access is more restrictive than protected : protected attributes and methods are still available by simply subclassing the class. Protected members are (or may be) intended to be inherited, while members with a closed package are not.

Frequent package members are often used, so multilpe classes within a package can access implementation-specific attributes or (utility) methods.

Good examples of this are the private-package String constructor and the StringBuilder.value char array:

 /* * Package private constructor which shares value array for speed. * this constructor is always expected to be called with share==true. * a separate constructor is needed because we already have a public * String(char[]) constructor that makes a copy of the given char[]. */ String(char[] value, boolean share) { // assert share : "unshared not supported"; this.value = value; } 

Thus, classes inside the java.lang can efficiently create new Strings if the content is already present in char[] without compromising security. You cannot do this from your application, because if you could, you would have access (reference) to the char String internal array, which is immutable (reflection does not count!).

In StringBuilder (or rather AbstractStringBuilder , where the implementation is implemented) the char array containing the current char[] value , and the access method for this char[] getValue() also batch, so various useful String methods like contentEquals(StringBuffer sb) and contentEquals(CharSequence cs) can use this for efficiency and faster comparisons without exposing the inner char array to the world.

0
Feb 10 '15 at 7:05
source share

A “private package” is used when you have several packages, which means that other classes in one package can access this class or class member as “public”, classes in other packages cannot access, for example, “private” ones. "

-one
Jun 24 2018-11-17T00:
source share



All Articles