Java Coding Style-part One

Source: Internet
Author: User

Java Coding Style:

1. Factory method should be stateless.

State normally refers to the member variables of class. Stateless, more precisely, it means immutable.

Factory was just to create objects, and one call should not affect anothers, if it was mutable, then the method call Mnight The chance to the States.
And then it would affect other calls which is not supposed to happen.

2.
Restrict the access level of any class members as much as possible, this conforms to principle of encapsulation

3.
Assert to validate the parameters of constructor, this is the avoid the invalid data of member variables as early as in th E Construction Stage
e.g. in Spring, if your assert to validate the values, Intead of leaving the error to later, application even won ' t be able To start the When Spring tries to initialise these objects.

4.
Try constructor to initialise the object, this is the validate the assigned values to member variables, y OU can construct the object
In one place.

e.g. in Spring, using constructor-to-init the beans, you can make the member variables as "final", this, the member VA Riables Get assigned and initialised
Early in the construction stage, you don't have a to create a empty object first and then set the fields using setters. What ' more, the member variables won ' is exposed to others
In this case, so you get a better access control over these member variables.

Java Coding Style-part One

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.