1. The sequence
In section 3, we learned how to enforce Singleton in different ways, but not all classes need to be instantiated. Sometimes, we may need to write classes that contain static methods and static fields. These classes get a bad rap because some people abuse them to write procedural programs in object-oriented languages.
However, they do have their own unique benefits:
- 1. Use these classes to organize primitive values and related methods on array types in the way of java.lang.Math or java.util.
- 2. We can also organize static methods, including factory methods, on objects that implement specific interfaces through java.util.collections.
- 3. You can use this class to organize methods on final classes instead of extending them.
These are some of the common utility classes that we do not want to be instantiated because instantiation means nothing to them.
2. Think about
1. So, can we define non-instantiable classes as abstract classes because we know that abstract classes cannot be instantiated? This is still not guaranteed, however, because its subclasses can still be instantiated.
2. There are some simple idioms to ensure that classes cannot be instantiated. Since the compiler generates a default constructor only if the class does not contain a display constructor, we simply make the class contain a private constructor and it cannot be instantiated:
// Noninstantiable utility class public class UtilityClass { // Suppress default constructor for noninstantiability private UtilityClass(( { throw new AssertionError(); }... // Remainder omitted }Copy the code
3. The advantages
Because the display constructor is private, it is not accessible outside the class. AssertionError is not required, but it can avoid accidentally calling the constructor inside the class. It guarantees that the class will not be instantiated under any circumstances. This idiom is a bit counterintuitive, as if the constructor is specifically designed not to be called. It is therefore advisable to add a comment to the code, as shown above.
4. The drawback
This idiom also has the side effect of preventing a class from having subclasses. Because all constructors of a subclass must explicitly or implicitly call the parent class’s constructor, in this case the subclass has no accessible parent constructor available.
5. Complete code
public class UtilityClass { // Suppress default constructor for noninstantiability private UtilityClass() { throw new AssertionError(); } public static UtilityClass getInstance() { return new UtilityClass (); } } public class Test { public static void main(String[] args) { UtilityClass one = UtilityClass .getInstance(); }}Copy the code
After the above code is executed, the following error is reported:
Exception in thread “main” java.lang.AssertionError at org.effectivejava.examples.chapter02.item04.UtilityClass.( UtilityClass.java:8 ) at org.effectivejava.examples.chapter02.item04.UtilityClass.getInstance( UtilityClass.java:13 ) at org.effectivejava.examples.chapter02.item04.Test.main( Test.java:9 )
Adding throw New AssertionError() successfully avoids instantiating UtilityClass classes in UtilityClass.
The following code execution can be successfully avoided:
public class SubUtilityClass extends UtilityClass { } public class Test { public static void main(String[] args) { // UtilityClass one = new UtilityClass(); }}Copy the code
When attempting to inherit the class, the following error is displayed:
Implicit super constructor UtilityClass() is not visible for default constructor. Must define an explicit constructorImplicit super constructor UtilityClass() is not visible for default constructor. Must define an explicit constructor
The client also cannot invoke the default constructor.
6. References
Blog.csdn.net/u012516166/… My.oschina.net/u/1034176/b…
Pay attention to the public account “Programmer interview”
Reply to “interview” to get interview package!!
This public account to share their own from programmer xiao Bai to experience spring recruitment autumn recruitment cut more than 10 offer interview written test experience, including [Java], [operating system], [computer network], [design mode], [data structure and algorithm], [Dacheng face by], [database] look forward to you join!!
1. Computer network —- Three times shake hands four times wave hands
2. Dream come true —– Project self-introduction
Here are the design patterns you asked for
4. Shocked! Check out this programmer interview manual!!
5. Teach you the interview “Profile” word for word
6. Nearly 30 interviews shared
7. Here are the free books you asked for