I've been reviewing Java Regex
Library, surprised by the fact the Pattern
class does not have a public constructor which I've taken for granted for years.
One reason I suspect the static compile
method is being used in favor of constructor could be that constructor would always return a new object while a static method might return a previously created (and cached) object provided that the pattern string is the same.
However, it is not the case as demonstrated by the following.
public class PatternCompiler {
public static void main(String[] args) {
Pattern first = Pattern.compile(".");
Pattern second = Pattern.compile(".");
if (first == second) {
System.out.println("The same object has been reused!");
} else {
System.out.println("Why not just use constructor?");
}
}
}
Any other strong rationales behind using static method over constructor?
Edit: I found a related question here. None of the answers there convinced me either. Reading through all answers, I get a feeling that a static method has quite a few advantages over a public constructor regarding creating an object but not the other way around. Is that true? If so, I'm gonna create such static methods for each one of my classes and safely assume that it's both more readable and flexible.
Java Pattern class doesn't have a public constructor, why? - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.
Java doesn't require a constructor when we create a class. However, it's important to know what happens under the hood when no constructors are explicitly defined. The compiler automatically provides a public no-argument constructor for any class without constructors. This is called the default constructor.
For the singleton pattern you use an private constructor to ensure that no other instances can be created, otherwise it wouldn't be a singleton.
A singleton class is a class in Java that limits the number of objects of the declared class to one. A private constructor in Java ensures that only one object is created at a time. It restricts the class instances within the declared class so that no class instance can be created outside the declared class.
Generally, a class won't have a public constructor for one of three reasons:
null
.In the class of Pattern
, the third case is applicable--the static compile
method is used solely for clarity. Constructing a pattern via new Pattern(..)
doesn't make sense from an explanatory point of view, because there's a sophisticated process which goes on to create a new Pattern
. To explain this process, the static method is named compile
, because the regex is essentially compiled to create the pattern.
In short, there is no programmatic purpose for making Pattern
only constructable via a static method.
This is just a design decision. In this case there is no "real" advantage. However, this design allows optimisation (caching for instance) without changing the API. See http://gbracha.blogspot.nl/2007/06/constructors-considered-harmful.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With