Java static, final qualifier

Here Insert Picture Description
Java static, final modifiers: the
difference between the two static variables and instance variables:
1. static variable is only a memory, the memory allocated to complete the process of loading a static variable in the class in memory can be accessed directly through the class name.

2. Create a new instance of each object will be assigned a different memory for the instance variables, each object access their own instance variables

3. No matter how many objects created a class of static variables are initialized only once, and all instances can access the static variable, and can be accessed directly through the class name.

类的成员变量有两种:静态变量和实例变量。前者是被 static 修饰的变量,后者是没有被 static 修饰的变量。

成员方法分为静态方法和实例方法。其中,使用 static 修饰的方法称为静态方法或类方法。

使用 final 修饰的变量表示取值不会改变的常量,经常使用“static final”类定义常量。

A, static
keyword "static" demonstration is: static.
. 1, static properties and methods can be modified in the class, the static properties of the modified variable called class, modified static method called class methods.
2, using a modified static member variables as static variables, using a modified static member methods as static methods, both of which can be accessed directly through the class name.
Two, Final:
Final modify variables when the value of the variable represents the unchangeable, called constants. For example, a round type comprising a PI (pi) property, and the value of this property will not change in either instance, the PI is defined as more in line with a constant programming requirements.

final data type variable name = value;

Wherein:
. 1, may be modified Final modifier static variables, local variables and instance variables, static constants respectively, examples of constants and local constants.
2, final type of variables must be explicitly initialized, otherwise it will cause a compiler error.
3, final variables can only be assigned once.
4, when defining the final constant, generally named in capital letters, and a plurality of words using an underscore "_" spaced

Published 31 original articles · won praise 0 · Views 370

Guess you like

Origin blog.csdn.net/weixin_45864941/article/details/103604334