Skip to main content
Add response to the comment
Source Link
sateesh
  • 28.9k
  • 7
  • 38
  • 45

Quoting from Effective JavaEffective Java, you can have a class with private constructor to have a utility class that defines constants (as static final fields).

(EDIT: As per the comment this is something which might be applicable only with Java, I'm unaware if this construct is applicable/needed in other OO languages (say C++))

An example as below:

public class Constants { private Contants(): public static final int ADDRESS_UNIT = 32; ... } 

EDIT_1: Again, below explanation is applicable in Java : (and referring from the book, Effective Java)

An instantiation of utility class like the one below ,though not harmful, doesn't serve any purpose since they are not designed to be instantiated.

For example, say there is no private Constructor for class Constants. A code chunk like below is valid but doesn't better convey intention of the user of Constants class

unit = (this.length)/new Constants().ADDRESS_UNIT; 

in contrast with code like

unit = (this.length)/Constants.ADDRESS_UNIT; 

Also I think a private constructor conveys the intention of the designer of the Constants (say) class better.

Java provides a default parameterless public constructor if no constructor is provided, and if your intention is to prevent instantiation then a private constructor is needed.

One cannot mark a top level class static and even a final class can be instantiated.

Quoting from Effective Java, you can have a class with private constructor to have a utility class that defines constants (as static final fields).

(EDIT: As per the comment this is something which might be applicable only with Java, I'm unaware if this construct is applicable/needed in other OO languages (say C++))

An example as below:

public class Constants { private Contants(): public static final int ADDRESS_UNIT = 32; ... } 

Quoting from Effective Java, you can have a class with private constructor to have a utility class that defines constants (as static final fields).

(EDIT: As per the comment this is something which might be applicable only with Java, I'm unaware if this construct is applicable/needed in other OO languages (say C++))

An example as below:

public class Constants { private Contants(): public static final int ADDRESS_UNIT = 32; ... } 

EDIT_1: Again, below explanation is applicable in Java : (and referring from the book, Effective Java)

An instantiation of utility class like the one below ,though not harmful, doesn't serve any purpose since they are not designed to be instantiated.

For example, say there is no private Constructor for class Constants. A code chunk like below is valid but doesn't better convey intention of the user of Constants class

unit = (this.length)/new Constants().ADDRESS_UNIT; 

in contrast with code like

unit = (this.length)/Constants.ADDRESS_UNIT; 

Also I think a private constructor conveys the intention of the designer of the Constants (say) class better.

Java provides a default parameterless public constructor if no constructor is provided, and if your intention is to prevent instantiation then a private constructor is needed.

One cannot mark a top level class static and even a final class can be instantiated.

Added explanation about applicability of this construct
Source Link
sateesh
  • 28.9k
  • 7
  • 38
  • 45

Quoting from Effective Java, you can have a class with private constructor to have a utility class that defines constants (as static final fields).

(EDIT: As per the comment this is something which might be applicable only with Java, I'm unaware if this construct is applicable/needed in other OO languages (say C++))

An example as below:

  

public class Constants { private Contants(): public static final int ADDRESS_UNIT = 32; ... } 

Quoting from Effective Java, you can have a class with private constructor to have a utility class that defines constants (as static final fields).

An example as below:

 
public class Constants { private Contants(): public static final int ADDRESS_UNIT = 32; ... } 

Quoting from Effective Java, you can have a class with private constructor to have a utility class that defines constants (as static final fields).

(EDIT: As per the comment this is something which might be applicable only with Java, I'm unaware if this construct is applicable/needed in other OO languages (say C++))

An example as below: 

public class Constants { private Contants(): public static final int ADDRESS_UNIT = 32; ... } 
Source Link
sateesh
  • 28.9k
  • 7
  • 38
  • 45

Quoting from Effective Java, you can have a class with private constructor to have a utility class that defines constants (as static final fields).

An example as below:

public class Constants { private Contants(): public static final int ADDRESS_UNIT = 32; ... }