Java Core Technology Chapter 4 - 2.final and static

final instance field

  Instance fields (properties of objects) can be decorated as final. After the modification is final, the instance field must be initialized when the object is constructed. If the instance field is not initialized, the instance field must be initialized in each constructor (otherwise a compile error will occur).

Indicates that it cannot be modified in subsequent operations. That is, there is no corresponding domain changer (set) method.

  Most of the final modification is applied to the field of basic type (basic data type) or the field of immutable class (for example: String class). If you modify the variable class, it may cause confusion to the reader.

 

static fields and static methods

static domain

  The property modified by static is called the static domain, which belongs to the class, so it is also called the class domain. There is only one such static field in each class. No matter how many times you new objects, they all share this static field,

So static fields belong to classes, not objects. You can see the picture below:

     

   I set the nextId as a static domain and userId as a private data domain. When calling the set method, the nextId will be incremented by 1. The following is the test method and results:

     

  You can see that the nextId of the UserInfo class is shared by both u1 and u2. When one of the objects modifies the nextId, the nextId of the other objects will also be modified.

 

static constant

   The above mentioned are static variables, which are generally used less, and more used are static constants, that is, properties modified by final and static.

For example, Math.PI (pi), the use method is class name. Property name Static constants generally modify immutable instance fields.

     

  In the previous chapter on encapsulation, it was said that it is best to set the instance field to private. The final modified constant is no problem, can be modified as public domain (public). Because final fields are immutable.

 

static method

  When using static methods, there is no need to instantiate the object. Use method as class name. Method name such as UserInfo.getNextId method

     

  When using the getNextId method, it does not use any UserInfo object, that is to say, its private data field is not used, and its static field can be accessed. And the above said that the static field belongs to the class, not to any object.

 

  Static methods are generally used in the following two situations:

  1. A method does not need to access the state of the object, and none of the required parameters use private data fields.

  2. A method only needs to access the static field of the class

 

  Mumu is just a Java novice. If the writing there is not good or if you have better suggestions, please leave a message or send it to my QQ mailbox [email protected]. Thank you everyone~(*^▽^*)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324874716&siteId=291194637