Good programmers teach you the right to avoid the java programming bug

Skills I: refuse initialization

  In Java, developers often carried out by the constructor to initialize the object. In fact, we can without having to call the constructor, by other means to achieve the object allocation.

(1) We can all variables declared private. To access an object outside of the class, you can use the GET and SET methods.

(2) for each object, write a new private boolean variable and initialize it.

(3) the preparation of a non-constructor of the class, which will ensure that each object is initialized before accepting calls.

Skills II: protection classes, methods and variables

  In the code among the classes, methods and variables are divided into public and private categories. Private classes can not easily accessible, public methods and variables are easy to approach and therefore often become *** breakthrough. Therefore, please limit its scope as possible.

Skills III: always on predefined range

  Most developers totally dependent on their packages range setting, in fact, we should adhere to the code predefined range. Many class itself is not completely closed, which means that it is able to impose *** ***. *** vulnerability can be inserted with a single class of their own, and then extracted sensitive information from the code. JVM that is not closed by default, but we were allowed to class enclosed in the package.

Four Skills: Avoid using inner classes

  In general, developers will use inner classes within the same package in other classes needed. These inner classes are usually available to all classes within the same package for a visit.

Principle V: Ensure that the class is not cloning

Java provides a feature for cloning of its own class when needed. However, this feature is often used by ***, copy the code from the example code and steal the necessary information.

  To solve this problem, we only need each class in the code, add the following code.


  If you want your class may have clonal, as far as possible while still avoiding security problems, then you can define cloning means of their own, and set it as final.


Guess you like

Origin blog.51cto.com/14249543/2403283