The static keyword
1 Overview
on the use of the static keyword, which can be used to modify the member variables and member methods, modified members belong to the class, not just belong
to an object. In other words, since belong to the class, you can not rely create objects called.
2 and using the format defined
class variables
when modified static member variable, which is called the class variable. Each object class share the same value of a class variable. Any object can change
operate on class variables when the value of such variables, but you can not create objects of that class in the.
* Class variables: the static keyword modified member variable.
Definition Format: static variable name data type;
Example: static int numberID;
For example, the base class opening new classes, students report. Now I want to every new report compiled classmates Student ID (sid), starting with the first students, sid is
1, and so on. Student ID must be unique, continuous, and consistent with the number of classes, so in order to know, to be assigned to a new student at school
number is how much. So we need a variable that has nothing to do with each individual student objects, but with the number of students throughout the class.
So, we can define a static variable numberOfStudent, code is as follows:
public class Student {
private String name;
private int age;
// 学生的id
private int sid;
// 类变量,记录学生数量,分配学号
public static int numberOfStudent = 0;
public Student(String name, int age) {
this.name = name;
this.age = age;
// 通过 numberOfStudent 给学生分配学号
this.sid = ++numberOfStudent;
}
// 打印属性值
public void show() {
System.out.println("Student : name=" + name + ", age=" + age + ", sid=" + sid);
}
}
public class StuDemo {
public static void main(String[] args) {
Student s1 = new Student("张三", 23);
Student s2 = new Student("李四", 24);
Student s3 = new Student("王五", 25);
Student s4 = new Student("赵六", 26);
s1.show(); // Student : name=张三, age=23, sid=1
s2.show(); // Student : name=李四, age=24, sid=2
s3.show(); // Student : name=王五, age=25, sid=3
s4.show(); // Student : name=赵六, age=26, sid=4
}
}
Static method
when the modified static member method called class methods. In a statement, static methods static, recommended to use the class name to call, without the need to
create an object class. Call is very simple.
* Class method: Use the keyword static member method modified, commonly called a static method.
Definition Format:
Static modifier return type method name (parameter list) {
// execute statement
}
Example: static methods defined in the class Student
public static void showNum() {
System.out.println("num:" + numberOfStudent);
}
Notes static method call:
- Static method to access class variables and static methods directly.
- Static methods can not directly access member variables or members of the general method. On the other hand, members of the method can directly access the class variables or static methods.
- Static method, you can not use this keyword.
Tips: static methods can only access static members.
Call format
is modified static and recommendations by members direct access to the class name . Although you can also access a static member by the name of the object, that is the reason more objects belong
to a class, use the same shared static members, but not recommended, a warning message appears.
format:
// access class variables
. Class name class variable name;
// call static methods
class name of the static method name (parameter).;
Call presentation, as follows:
public class StuDemo2 {
public static void main(String[] args) {
// 访问类变量
System.out.println(Student.numberOfStudent);
// 调用静态方法
Student.showNum();
}
}
3 illustrates the principle of static
static modification of content:
- As the class is loaded and loaded and loaded only once.
- Stored in a fixed memory area (static area), so you can directly call the class name.
- It takes precedence over the object exists, it can be shared by all objects.
4 static code block
Static block of code: member position is defined using a modified static block {}.
- Location: outer class method.
- Execution: with the loaded class is executed and performs a priority for performing the method of the main methods and constructors.
format:
class ClassName {public
static {
// statements executed
}
}
Role: variables are initialized to the class assignment. Usage demo code is as follows:
public class Game {
public static int number;
public static ArrayList<String> list;
static {
// 给类变量赋值
number = 2;
list = new ArrayList<String>();
// 添加元素到集合中
list.add("张三");
list.add("李四");
}
}
Tips:
static keyword, the variables may be modified, methods and code blocks. In the use of the process, its main purpose is to want without creating an object
, the method to call.