The use of the static keyword in java learning summary

static usage

1.static can only be used to modify the methods and variables can not be used to decorate a class, the class can only use the keyword public, abstract and final modified

2.static refers to a static variable, it only allocate a memory space, used only once, not redistribution of memory space to store the next time you use the variable value, the value will only replace the last.

3. The use of static local variable is not allowed to modify the

4. Call the object by name. Method () / variable or using the class name. Method () / Variable

E.g:

 

package com.encapsulation;

public class CatTest {

	public static void main(String[] args) {
		System.out.println("第一支小猫的id\n第二支小猫的id");
		Cat one = new Cat();
		Cat two = new Cat();
		one.id = 1;
		two.id = 5;
		System.out.println(one.id + "," + two.id);
	}

}

5. You can not use static methods to access non-static method

6. The construction block of code: class which is present, and not added directly define the key code block is called static {} class configured in the code block. Code block is configured to be called when the object is created each time will be called to create an object, and the execution order of the code block structure in preference to the class constructor .

Published 44 original articles · won praise 15 · views 20000 +

Guess you like

Origin blog.csdn.net/IGGIRing/article/details/88389823