Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effective Java: Make constructors private or package-private

From Chapter 4 of Effective Java the following example is given:

public class Complex { 
    private final double re; 
    private final double im;

    private Complex(double re, double im) {
        this.re = re;
        this.im = im;
    }

    public static Complex valueOf(double re, double im) {
        return new Complex(re, im);
    }
    ... // Remainder unchanged
} 

[...] It is the most flexible because it allows the use of multiple package-private implementation classes. To its clients that reside outside its package, the immutable class is effectively final because it is impossible to extend a class that comes from another package and that lacks a public or protected constructor.

I understand that the class is effectively final,

but is there any actual advantage from not declaring it final?

Omitting this keyword when the class doesn't support extension seems like a less clear way to communicate how the API is meant to be used. Or was final only left out here to show the reader that non-extendable, non-final classes are possible?

like image 637
flakes Avatar asked Mar 17 '16 15:03

flakes


People also ask

Should Java constructors be private?

Yes, we can declare a constructor as private. If we declare a constructor as private we are not able to create an object of a class. We can use this private constructor in the Singleton Design Pattern.

Should constructors be public or private?

Constructors in programming are the methods that are called automatically when an object is initialized. The constructor's purpose is to initialize the object. Constructors should always be public and they are declared without any return type.

Why should constructors be private?

Private constructors are used to prevent creating instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class.

Can a constructor be private or protected?

Access specifiers/modifiers allowed with constructorsModifiers public, protected and, private are allowed with constructors. We can use a private constructor in a Java while creating a singleton class. The Singleton's purpose is to control object creation, limiting the number of objects to only one.


1 Answers

The main benefit is to limit the scope of inheritance. Declaring it final will make it sealed everywhere; however, sometimes as an author of the class you would like to inherit the class within the same package or class file but you don't want others to do the same. And that's what this trick will do.

It's a nightmare to change a class that is open for inheritance (Commonly used APIs). If you can limit the scope of inheritance, then it becomes a simple refactoring job.

like image 155
Sleiman Jneidi Avatar answered Oct 25 '22 15:10

Sleiman Jneidi