Effective Java Learning Notes (i)

Source: Internet
Author: User
Tags constructor

Recently in the watercress to see some book reviews, very feel, and there is a call effective Java is said to be very good, download a look, really masterpiece, a lot of things are not read, the skill is still shallow, still have to continue efforts. It feels so good to read that it's much better than a grammar-telling book.

The first part of the object is created and destroyed.

Said 5 principles.

1. Replace the constructor with a static factory method.
Benefits: Avoid creating duplicate objects, because static methods are not allowed to create objects that can be used, causing unnecessary waste. Returns the same object repeatedly. Ensure a single state.
A factory method can take a name that is easier to remember, but a constructor is not.
The factory method can return a subclass object of the original return type, suitable for an interface based framework structure.
Disadvantage: Classes cannot be quilt-class if they do not contain public or protected constructors. The same is true for Non-public classes that are returned by a publicly owned static factory.
Naming habits: valueof (), getinstance ()
2. Use private constructors to harden Singleton.
Singleton represent system components that are inherently unique.
Two methods of implementation. One is the static final class object that defines the public.
The second is to return an object using the method. There is also the time to serialize should pay attention to the content of this aspect.
3. Enhance the ability to not instantiate through private constructors.
For some tool classes, only static methods and static fields are included, and constructors are defined as private to improve the performance of the system.
4. Avoid repeating the creation of duplicate objects.
The cost of creating objects in a loop is very expensive, so avoid creating duplicate objects, adding static fast static{}, and defining final constants, which reduces the definition of duplicate objects. Improve performance. In the object pool, especially the database connection, should pay attention to the prevention of the postback object, and improve the system performance efficiently.
5. Eliminate references to expired objects.
Tells a memory leak example. Very classic stack, when the stack pops up often expired references, because the stack manage their own memory.
In general, as long as a class manages its own memory, the programmer should be wary of memory leaks, and once an element is released, the reference to any object contained within that element should be emptied.
Another source is caching. Once an object's reference is placed in the cache, it is easily forgotten.

With a limited level, I wrote the little things that I understood.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.