About objective Java Distilled:
I have read this book tive Java for nearly two times on and off, and the content in it is very deep and helpful for improving the quality of engineering code. I plan to sort out a series slowly. The reason why I name it objective Java distilled is to organize the essence of this book as much as possible to facilitate review and use. After all, my memory is very limited, many things are often forgotten. In a word, it's just some reading notes for this book. The content in the article must be loyal to the original text. For some items, some content may be added, and I will mark the added content. At the same time, I hope this series of articles will help you. If you have any questions or suggestions, please leave a message. Thank you.
Outline:
- Classes do not need to be instantiated
This item may have the least content among all items. But it also covers several techniques that are commonly used in our daily coding. It is used when you create a tool class.
Classes do not need to be instantiated
In a word, it is completely used to provide the type of static tool methods and does not need to be instantiated. In JDK, there are several typical examples. For example, java. util. arrays, java. util. Collections, java. Lang. Math. The Declaration methods of these classes are as follows:
public class Arrays { // Suppresses default constructor, ensuring non-instantiability. private Arrays() { } …}public class Collections { // Suppresses default constructor, ensuring non-instantiability. private Collections() { } …}public final class Math { /** * Don't let anyone instantiate this class. */ private Math() {} …}
The above code snippets are all taken from the JDK source code. We can see that they all use a private constructor, which has no internal logic. This is to tell the compiler not to generate a default non-argument public constructor for this type during compilation, that is, if a class does not provide any constructor, the compiler generates a default constructor for it. Once Any constructor is provided (any access level is acceptable), the compiler will not provide the default constructor.
In addition, we have noticed that the declaration method of the above math class is different from those of collections and arrays classes. The declaration of the math class uses the final keyword. Does this keyword have any special significance? Thinking
In Java, the final keyword section in the reusing classes chapter explains the semantics of final keywords in various scenarios. When final is used to modify the class, it indicates that the class cannot have child classes. In the final-modified types, all methods are final, because they cannot be overwritten by the quilt class.
Therefore, in this scenario, since we have provided a unique private constructor for the type, it means that the type cannot have any subclass, the reason is that the subclass constructor must explicitly or implicitly use the non-private constructor of the parent class. Therefore, in this scenario, using final or not produces the same effect.
Therefore, the declaration of the math class is modified by the final keyword, but arrays and collections are not. I think it may be because everyone's coding habits are different from the way of thinking ...... The author of the math class is Joseph.
D. Darcy, two authors of the last two classes, Joshua Bloch and Neal gafter. They co-authored an interesting Java puzzlers and Joshua
Bloch is the author of objective Java.
Notes
It is incorrect to declare a type as an abstract class so that the type cannot be instantiated. Because the abstract class is intended to inherit from the quilt class, And the constructor of the subclass will call the constructor of the abstract class, the abstract class itself is indirectly instantiated.
In addition to using a private constructor, you can also add a line of code in the constructor to reference objective Java item
Code segment 4:
// Noninstantiable utility classpublic class UtilityClass {// Suppress default constructor for noninstantiabilityprivate UtilityClass() {throw new AssertionError();}... // Remainder omitted}
That is, you can throw an asserted class exception to prevent the private constructor from being inadvertently called in this type. At the same time, this method can also call the private constructor say no for reflection. This technique is also useful in item3 for the implementation of Singleton classes.