Java Language Learning summarizes the advanced version of the static keyword meaning and method of use

The static keyword

Modified static member variables

meaning

Define a static variable, it is common to belong to the class, but the class object is not unique, all objects share a message.

Examples

The establishment of a student class, a student's name, age, school number three basic like, belonging to each student privately owned, but each student in the same dorm, all the room number is public, that is, each student jointly owned.
Establishing the following categories, as follows:

public class StudentTe {
	private int id; // 学号
	private String name; // 姓名
	private int age;
	static String room;
	private static int idCounter = 0;
	
	public StudentTe() {
		this.id=idCounter++;
	}
	
	public StudentTe(String name,int age) {
		this.name= name;
		this.age=age;
		this.id=++idCounter;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public static String getRoom() {
		return room;
	}

	public static void setRoom(String room) {
		StudentTe.room = room;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

idCounter counter equivalent to the number of students automatically calculates created. The public is also this very common

An object created with StudentTe class as follows, and object initialization:


public class TestStudent {

	public static void main(String[] args) {
		StudentTe one = new StudentTe("郭靖",191);
		one.room="101教室";
		System.out.println("姓名:"+one.getName()+ ",年龄:"
				+ one.getAge() + ",教室:"+one.room + 
				",学号:"+one.getId());
		
		StudentTe two = new StudentTe("黄蓉",16);
		System.out.println("姓名:"+ two.getName() + "年龄:"
				+ two.getAge()+",教室:"+two.room + 
				", 学号:"+ two.getId());
	}

}

Output results are as follows:
Output
seen on the static variables need to set the room only once, while each creates an object id to +1.

Modified static member methods

Modification using static member methods, has become static methods, static methods are not objects, but belong to the class of

And when you call the static method static definition, need to use the type of static method format.
When calling method does not modify the static keyword, you must define the object, using the object method format.
Sample code, the following method StudentTe added to the above mentioned class, as follows:

public static void methodStatic() {
		System.out.println("这是静态方法");
	}
	public void method() {
		System.out.println("这是非静态方法");
	}

Adding the following code modal class:

		StudentTe stu =new StudentTe();
		stu.method();
		StudentTe.methodStatic();

Results are as follows:
Output
static method can also use the object static methods to call, but not recommended.

With a static modification of static variables and static methods can be used class name. Static variables / methods .

Precautions

  1. Static can directly access only static, can not directly access non-static. Because the memory of them is to access static content, re-access non-static content. Ancestors descendants do not know, future generations know their ancestors
  2. Static methods can not use this keyword.

Static static memory

There is a static area in memory static variables and methods to specifically store.
When accessing a static variable or static method, heap memory is not to find, but directly to the memory area in a static method lookup zone.
FIG memory as follows:
Memory map

Static code block

Among the direct write inner code block
format definition:
public class class name {
static {
// static block of code
}
}

Sample code is as follows:

public class TestStudent {
	
	static {
		System.out.println("静态代码块执行!");
	}
}

Features:
When used in this first class, the only time a static block of code.

Static content is always take precedence over non-static, the static code block construction method than prior execution

Purpose:
to give a one-time static variable.

——————————————————————————————

Published 50 original articles · won praise 3 · Views 5178

Guess you like

Origin blog.csdn.net/Ace_bb/article/details/104072741