1-static的引出:可以由多个对象使用,不仅仅只是由某一个对象使用,即对象都具有的属性或者方法都可以用static关键字定义
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e550fab34cb77aab6bc061fca9d5c000.png#pic_center)
2-如果一个成员变量使用了static关键字定义,那么这个变量不再属于自己,属于所在的类。多个对象共享同一个数据。
2.1)定义一个学生类Student
public class Student {
private String name;
private int age;
static String room;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
}
2.2)测试学生类
public class Demo01Static {
public static void main(String[] args) {
Student one = new Student("王三",20);
one.room = "1601";
System.out.println("姓名:"+one.getName()+"年龄:"+one.getAge()+"所在教室:"+one.room);
System.out.println("================");
Student two = new Student("Lisa",25);
System.out.println("姓名:"+two.getName()+"年龄:"+two.getAge()+"所在教室:"+two.room);
}
}
3-static可以用来解决 学号自动增长
public class Student {
private int id;
private String name;
private int age;
static String room;
static int idCounter=0;
public Student() {
++idCounter;
}
public int getId() {
return id;
}
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 class Demo01Static {
public static void main(String[] args) {
Student one = new Student("王三",20);
one.room = "1601";
System.out.println("姓名:"+one.getName()+", 年龄:"+one.getAge()
+", 所在教室:"+one.room+", 学号:"+one.getId());
System.out.println("================");
Student two = new Student("Lisa",25);
System.out.println("姓名:"+two.getName()+"年龄:"+two.getAge()+
"所在教室:"+two.room+", 学号:"+two.getId());
Student three = new Student("Rose",25);
System.out.println("姓名:"+three.getName()+"年龄:"+three.getAge()+
"所在教室:"+three.room+", 学号:"+three.getId());
}
}
4-static修饰方法
public class MyClass {
int num;
static int numStatic ;
public void method(){
System.out.println("这是我的成员方法!");
System.out.println(num);
System.out.println(numStatic);
}
public static void methodStatic () {
System.out.println("这是我的静态方法!");
System.out.println(numStatic);
}
}
public class Demo02MethodStatic {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.method();
MyClass.methodStatic();
System.out.println("===========");
myMethod();
}
public static void myMethod(){
System.out.println("这是本类中的静态方法!");
}
}
5-static内存图
public class Demo03Static {
public static void main(String[] args) {
Student.room="创造101";
Student one = new Student("郭靖",20);
System.out.println("one的名字:"+one.getName());
System.out.println("one的年龄:"+one.getAge());
System.out.println("one的教室:"+Student.room);
System.out.println("=============");
Student two = new Student("黄蓉",18);
System.out.println("two的名字:"+two.getName());
System.out.println("two的年龄:"+two.getAge());
System.out.println("two的教室:"+Student.room);
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/88438d9c219d4efe45cabff45cc57a45.png#pic_center)
6-静态代码块
public class Person {
static {
System.out.println("我只被执行一次!");
}
}
public class Demo04Static {
public static void main(String[] args) {
Person p1 = new Person();
System.out.println("========");
Person p2 = new Person();
}
}