package cn.itcast.day09.demo01;
public class Student {
private String name;
private int age;
static String room;
private int id;
private static int idCounter = 0; / / student number counter, each new new object count++
public Student(){
this.id = ++idCounter;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
this.id = ++idCounter;
}
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 int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
package cn.itcast.day09.demo01;
/* If the member variable uses the static keyword, then the variable belongs to the class */
public class Demo01StaticField {
public static void main(String[] args) {
Student one = new Student("gj",20);
one.room = "101";
System.out.println("name:"+one.getName()+" age:"+one.getAge()+" room:"+one.room);
System.out.println("id is:"+one.getId());
Student two = new Student("hr",21);
System.out.println("name:"+two.getName()+" age:"+two.getAge()+" room:"+one.room);//static decoration, all class sharing
System.out.println("id is:"+two.getId());
}
}
package cn.itcast.day09.demo01;
The /*static modifier member method becomes a static method, does not belong to an object, and belongs to a class.
* No static modification must first create an object to use
* Static can not access non-static: in memory, existing static content has non-static content
* Static methods cannot use this*/
public class Demo02StaticMethod {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.method();/ / Create an object before you can use
/ / Static methods can be called with the object name, can also be called directly by the class name
obj.methodStatic();/ / Not recommended, will be translated into class name by javac after compilation. Static method name
MyClass.methodStatic();
myMethod();/ / Direct call
}
public static void myMethod(){
System.out.println("Own method"); / / Static methods in this class can omit the name
}
}
package cn.itcast.day09.demo01;
public class MyClass {
public void method(){
System.out.println("This is a member method");
}
public static void methodStatic(){
System.out.println("This is a static method");
}
}
package cn.itcast.day09.demo01;
/* static code block */
public class Person {
static {
System.out.println("Static method is called");/ / The first time you use this class, the static method call is only one time
}
public Person(){
System.out.println("Construction Method Execution");
}
}
package cn.itcast.day09.demo01;
/* static method assigns static variables at once */
public class Demo03Static {
public static void main(String[] args) {
Person one = new Person();// static content first
Person twp = new Person();/ / Static method is no longer executed
}
}
table of Contents Overview of static keywords The static keyword modifies member attributes Static keyword modification member method Precautions Static memory map Static code block The format of the ...
Static agent You: true role Wedding company: Acting you, help you deal with marriage Marriage: achieving a marriage interface...
1 Encyclopedia explanationstatic: Sometimes you want to define a class member so that its use is completely independent of any objects of the class. Under normal circumstances, a class member must be ...
1. Static keyword (1) Modify member variables: If a member variable uses the static keyword, the variable no longer belongs to the object itself, but belongs to the class it belongs to, that is, multi...
Basic knowledge 1. The difference between String, StringBUffer and StringBuilder Variability In the String class, the final keyword is used to modify the character array to save the string. The privat...
Static library: In the connection phase, it will be connected with the target code to form an executable program. Suffix: .a. Similar to the .lib file under the Windows platform. 1. Preparation (1) Se...
References: C/C++ Standard Design Concise Course, P287 Programming environment: VS2017 Step 1: Create a static library project, the project name is "StaticLib003" Step 2: Add header file Int...
Java study notes - inner classes and static modifiers Internal class Internal class(inner class) is a class defined in another class. Features of the inner class: The inner class is a compile-time syn...
In the class with public static final variables modified Description: It is to facilitate public access static is to let it become a member of the class, rather than members of the object. Static vari...
A, this: this effect: 1, to distinguish local variables and member variables; 2, refers to the object itself;Method must be called by the object By following the code explain: Method call, if this exi...