Directory:Effective java--End Method GuardianEffective Java Chapter 2nd: Creating and Destroying objects. 7th: Avoid using the finalization method. The last example of the "End Method Guardian (finalizer Guardian)" to deepen understanding.1 /**2 * Chapter 2--end Guardian3 * @ClassName: Parent4 * TODO5 * @authorXingle6 * @date 2015-3-11 pm 3:49:477 */8 Public classParent {9 Ten Public Static void
Generics are a feature introduced by Java in the JDK1.5 version, which greatly enhances the security of Java code runtime, where generics are primarily used in container classes, because these classes contain a wide variety of other classes, so you need to use generics to constrain the classes contained in the container, such as passing in a string type parameter to the list Number, then, the list object ca
policy with a function objectIn Java, where there is a similar set of sequencer, the Sequencer object only represents the strategy of sorting, such instances of the class is functionally not different, mutual equivalence, suitable as a singleton, saving unnecessary object creation overhead.prioritize static member classesStatic member classes are the simplest kind of nested classes. If the member class does not require access to the perimeter instanc
JNI calls.These JNI methods is coded in C or C + +. They do the actual work, interacting with a native label.Let's look at the figure.You can see this AWT components provide a universal public API to the application by AWT component class and AWT peers. A Component class and its peer interface is identical across platform. Those underlying peer classes and JNI codes are different. )
In addition, finalizer chaining was mentioned. The subclass constructor automatically calls the parent class con
Today just began to read effective Java, the Chinese version of the reading is very awkward, but feel a lot of harvest.In addition, the content of this book is for Java 1.5 and 1.6.Here's the 2nd Chapter: Creating and destroying the contents of an object.First: Consider replacing the constructor with a static factory methodThis article is intended for scenarios w
"Effective Java" catalog excerpt.I know it looks bad. In the present, the lack of actual operation, can only temporarily extract the directory. With the increase in practice, fill in more examples slowly.Chapter 2 Creating and destroying Objects
Consider static factory methods instead of constructors
Consider a builder when faced with many constructor parameters
Enforce the Singleton proper
Before reading this book "Effective Java (second edition)" is very early. This book is indeed a good book. You need to chew slowly and have a different experience every time you look.Write a blog here to consolidate.Chapter I. Creating and Destroying objects Consider replacing the constructor with a static factory methodUsually we get an instance of the class, and the more frequently used method is to use t
There are two mechanisms for Java to provide multiple implementations of an abstraction-Interface and abstract class.Interface and abstract class,In addition to the obvious differences (i.e., the ability to provide a basic implementation),The important difference is that the implementation class for an interface can be at any point in the class hierarchy, and the subclass of the abstract class is subject to this limitation.
Existing classes c
Directly on the code:1 Public enumBoss {2 3 INSTANCE;4 5 PrivateString name;6 7 Public voiddosomething () {8SYSTEM.OUT.PRINTLN (name + "is doing something now ...");9 }Ten One PublicString GetName () { A returnname; - } - the Public voidsetName (String name) { - This. Name =name; - } - +}Note: The enum in Java is a thoroughly-bottomed class that can be freely added to other properties and methods, so
corresponding domain name queue, send and update the last send time of the changed domain name.2. Unlock the domain to send records.In this way, the non-blocking queue is used for message queue, which improves the concurrency of the system.It seems to solve the problem, but it was blacklisted by some service providers.The system is then analyzed as follows:This is a typical producer-to-consumer issue in which Java client-produced messages are timed t
With the effective Java this book to see more and more, found in peacetime use of Java made a lot of obvious mistakes, they usually know a little but the lack of use is also more and more.As for enumeration classes, the suggestion in the book is to replace the traditional static constants with enumeration classes, usually, if a class of data is represented by a d
Create | objects
Effective Java Learning notes the Java language supports four basic types: interface (Interface), Class (Class), array (array), and primitive type (primitive). The first three types are often referred to as reference types (reference type), instances and arrays of classes are objects (object), and the values of primitive types are not objects. A
Sixth elimination of expired referencesThere will still be memory leak in Java, such as a stack growth and contraction, then the object pops from the stack will not be garbage collected, the immediate use of the stack of the program no longer reference these objects. This is because the inside of the stack maintains outdated references to these objects (which are never dismissed) and needs to be shown to empty (null) the objects and tell the garbage c
rather than classes to refer to objects.2. If no interface is available, consider the abstract base class.Ix. interface takes precedence over reflection mechanism1. Defect of reflection mechanismA, loss of the benefits of compile-time type checkingb, the code needed to perform the reflection access is very clumsy and lengthy.C, Performance loss2, usually only the class must work with the class that is unknown at compile time to need reflection. Used only to instantiate objects, it is best to us
The first thing about using the OO language in Java is that if the class is not designed to be inherited, try not to use the combinationFrom 2, our class B makes a copy of the add of Class A to drink the AddAll method, so that we can count the number of calls to Class A every time we call it, and we actually call Class B.But the count variable of instance object B becomes 2, explaining the reason in Figure 1 I have done a blackboard painting, this exa
an alternative to choose the following methods:Private Static Final thing[] Private_values = {...}; Public Static Final ListPrivate Static Final thing[] Private_values = {...}; Public Static Final thing[] VALUES () { return private_values.clone ();}9. In Java, public is not the highest access level, and if it is not in the export package, it will be private to the module.10. The public class should not have a public domain unless it is a public
Effective Java in the requirements of the class, interface, etc., summed up is enough to do, do not give too much access, the definition of the class is also, if a class is only used inside the class, then the class is defined as an inner class bar.The inner class is divided into four types:1, static inner class: Static inner class is in front of class more static keyword inner class, this class and class s
construction Process
There is a cost to creating the builder
//Example Builder mode Public classnutritionfacts {Private Final intServingsize;//Required Parameters Private Final intservings;//Required Parameters Private Final intFat//Optional Parameters Private Final intSodium//Optional Parameters Public Static classBuilder {Private Final intservingsize; Private Final intservings; Private intFat = 0; Private intSodium = 0; PublicBuilder (intServingsize,intservin
is asked to create a second instance. In addition to the factory method there is a public domain method:public class Elvis2 {public static final Elvis2 INSTANCE = new Elvis2 ();p rivate Elvis2 () {}public void leavethebuilding () {} }The advantages of this approach are clear and unambiguous. The disadvantage is rigid, performance is not. The advantage of factory method is flexibility. The disadvantage is that it can be easily modified, such as changing to a unique instance of each thread that i
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.