Java variables and the final keyword

Foreword

Here is not to talk about what the basic data type or reference type, the main variable is initialized to understand some small points


Java variables

For Java variables, depending on the position into local variables and member variables:

  • Local variables: methods and definitions of the variables within the block of statements (except parameter, the local variable must be explicitly initialized, if not initialized, not by the compiler). Local variables divided into three types: (variables defined method signature) (1) parameter: the entire scope of the method; when an object or class calls a method, the method in the system for all the stack area shape reference allocate memory space, and assign a corresponding value of the argument parameter, thus completing the initialization parameter; (2) a method of local variables (as defined in method): where the definition of the scope from the local variable became effective , the disappearance of the end of the process; (3) block local variables (defined in block): starting from the scope of the local definition of the variable force, the end of the block to fail;
  • Member variables: internal methods defined outside of the class variables. Class member variables and member consists of methods. The member variables are divided into instance variables and class variables. Use static member variables are modified class variables, belong to the class itself; do not use modified static member variable is an instance variable, are instances of the class. Since only one corresponding to each class in the same JVM a Class object, and therefore a class within the same JVM class variable only a memory space; but for instance variables, the class created once every instance, it is necessary for the instance variables allocate a block of memory space. In other words, there are several instances of the program, instance variables we need a few pieces of memory space.

Detailed final keyword

  1. When the modified class: when used to modify a final class, indicates that the class can not be inherited. Note:. A modified class is final, final class member variables can be designed to fianl according to their actual needs. B. A method final class members will be the final method implicitly designated. Description: In a class of their own design when, in order to be good whether to extend this class in the future, if you can be inherited, the class can not be used fianl modification, over here, in general, we tend tools are designed to be a fianl class. In the JDK, it is designed to have a String, System and other final class.

  2. Modification methods: The final modified method can not be overridden. Note:. A private method implicitly class is designated as the final method. b. If the final modification of the method of the parent class, then the subclass can override go.

  3. When modifying local variables: can only be assigned once (if no assignment initialized before use must be one and only one assignment), Also, if a final variable assignment at initialization, then the subsequent use of, as the following code :

    public static void main(String[] args) {    
    		//进行宏替换的final变量 
    		final int a=5; 
    		//这里再运行过程中直接将a转化成5,这就是“宏变量”,必须再定义是才有效果
    		// 我自己试着直接将class文件反编译后,发现a被直接替换为5了
    		/*
    		 * 如果一个final满足这三个条件,那么他就相当于一个直接量
    使用final进行修饰
    在定义final变量的时候进行了初始化。
    该初始值在编译的时候就可以确定下来。
    是不是看出来一个很重要的点,那就是在定义时就进行初始化。
    		 */
    		System.out.println(a);    
    		
    	}
    

    This code after decompile:

    public static void main(String[] args) {    
    		int a=5; 
    		System.out.println(5);    
    		
    	}
    

    It can be seen directly replaced with a variable whose value is 5. This is the macro substitution.

  4. Modified member variable: is assigned fianl modified member variable, divided into two situations:
    the first one is the final modification instance variables. This final initialization is performed three places: first, initialized directly defined; the second one, carried out in the code block; the third, in the configuration process.
    The second is the final variable that is modified class member variables at this time is static, final modifications at the same time. Such variables are initialized in two places: first, the direct definition; second, static code block types.

Published 14 original articles · won praise 15 · views 525

Guess you like

Origin blog.csdn.net/CodingNO1/article/details/104307475