- 前言
- static修饰成员变量
- 静态成员变量的初始化
- static修饰成员方法
- static修饰符总结
- 静态成员方法总结
- 静态成员变量总结
我们知道一个类中是有成员的,比如成员变量是描述类的属性的,成员方法可以用来描述类的行为,那么假如每一个对象的中的一个成员属性或者成员方法是共享的,怎么定义的呢?
static修饰成员变量static修饰的成员变量,称为静态成员变量,静态成员变量最大的特性:不属于某个具体的对象,是所有对象所共
享的 。
class Student { public String name; public int age; public int height; public static String address = "海底世界"; public Student(String name, int age, int height) { this.name = name; this.age = age; this.height = height; } } public class test { public static void main(String[] args) { Student student1 = new Student("派大星",18,175); System.out.println("姓名:" + student1.name); System.out.println("年龄:" + student1.age); System.out.println("身高:" + student1.height); System.out.println("班级:" + student1.address); System.out.println(); Student student2 = new Student("海绵宝宝",15,165); System.out.println("分割线-------------------------------------"); System.out.println(); System.out.println("姓名:" + student2.name); System.out.println("年龄:" + student2.age); System.out.println("身高:" + student2.height); System.out.println("班级:" + student2.address); } }
运行结果:
从箭头指向的两个班级来看,我们可以得知static修饰的成员变量是共享的。
在Java中静态成员变量是不输入任何的一个对象的,是属于类的。所以更推荐用类名来直接访问静态成员变量。
class Student { public String name; public int age; public int height; public static String address = "海底世界"; public Student(String name, int age, int height) { this.name = name; this.age = age; this.height = height; } } public class test { public static void main(String[] args) { Student.address = "中国台湾省"; System.out.println(Student.address); } }
运行结果:
静态成员变量的初始化成员变量我们知道是可以不用初始化的,因为它会默认初始化成对应的0值,但我们最好还是要初始化比较好。那么静态成员变量怎么初始化呢?答案是用在静态代码块中初始化。
class Student { public String name; public int age; public int height; public static String address = "海底世界"; static { address = "CHINA"; } public Student(String name, int age, int height) { this.name = name; this.age = age; this.height = height; } } public class test { public static void main(String[] args) { System.out.println(Student.address); } }
在红框中的代码块就是静态代码块,在代码块中进行了对静态成员变量的初始化,那么我们运行一下看看结果
打印出来的结果是CHINA,那么就是说在静态成员变量的就地初始化中,是被覆盖成了CHINA,那么也就是说先执行静态成员变量的就地初始化,再执行静态代码块的。假如我们把它们的顺序调转,运行的结果又是怎么样的呢?
class Student { public String name; public int age; public int height; static { address = "CHINA"; } public static String address = "海底世界"; public Student(String name, int age, int height) { this.name = name; this.age = age; this.height = height; } } public class test { public static void main(String[] args) { System.out.println(Student.address); } }
可以看出打印的是另外一个结果,那么也就是说明,在同时有静态成员变量和静态代码块的同时,是按他们定义的顺序执行语句的。
static修饰成员方法与修饰成员变量一样的语法原理,static修饰的成员方法不属于任何一个对象,是属于类的,所以static修饰的成员方法可以直接用类名访问。
class Student { public String name; public int age; public int height; public static String address = "海底世界"; public static void print() { System.out.println("这是一个static修饰的成员方法"); } } public class test { public static void main(String[] args) { Student.print(); } }
运行结果:
static修饰符总结 静态成员方法总结1 . 不能在静态方法中访问任何非静态成员变量
用this修饰也不行!
原因:是因为静态成员方法不依靠对象,只依靠类,你试想一下,普通成员变量是实例化才会分配内存空间的,而你没有实例化,怎么去访问?
假如你还是想在静态方法中访问普通成员变量,可以,那就在静态方法中实例化一个对象出来,然后通过这个对象的引用来访问。
class Student { public String name; public int age; public int height; public static String address = "海底世界"; public static void print() { Student student = new Student(); student.name = "padaxing"; System.out.println(student.name); System.out.println("这是一个static修饰的成员方法"); } } public class test { public static void main(String[] args) { Student.print(); } }
2 . 静态成员方法不属于某个具体的对象,是类方法
属于类的方法,可以直接用类名调用
class Student { public String name; public int age; public int height; public static String address = "海底世界"; public static void print() { Student student = new Student(); student.name = "padaxing"; System.out.println(student.name); System.out.println("这是一个static修饰的成员方法"); } } public class test { public static void main(String[] args) { Student.print(); } }
3 . 也可以通过对象名调用静态成员方法,不过比较推荐使用类名
1. 不属于某个具体的对象,是类的属性,所有对象共享的,不存储在某个对象的空间中2. 既可以通过对象访问,也可以通过类名访问,但一般更推荐使用类名访问3. 类变量存储在方法区当中4. 生命周期伴随类的一生(即:随类的加载而创建,随类的卸载而销毁)