Java static关键字
1.static:静态的
static修饰的成员变量和方法从属于类,而普通变量和方法从属于对象。
2.static可以用于修饰
属性,方法,代码块,内部块。
3.使用static修饰属性:静态变量(类变量)
3.1属性 按是否使用static修饰,又分为:静态属性和非静态属性(实例变量)
实例变量:指没有被static修饰,每个对象独有
静态变量:指被static修饰的成员变量,多个对象共享一个静态变量
3.2static修饰属性的其他说明:
1.静态变量随着类的加载而加载
2.静态变量的加载早于对象的创建
3.由于类自会加载一次,则静态变量在内存中也只会存在一份,存在于方法区的静态域中
4.使用static修饰方法
4.1随着类的加载而加载,可以通过类.静态方法调用
4.2静态方法中,只能调用静态的方法或属性
非静态方法中,既可以调用非静态方法或属性,也可以调用静态的方法
5.static注意点
5.1在静态方法内,不能使用this,super 关键字
5.2关于静态属性和静态方法的使用,都从生命周期的角度去理解
6.1开发中,如何确定一个属性是否要声明为static?
1.统一公共属性,不随对象的不同而改变
2.final修饰的常量,通常也声明为static
6.2开发中,如何确定一个属性是否要声明为static?
1.操作静态属性的方法,设置为static
2.工具类中的方法,通常设置为static(没有对象也可以使用)
7.静态代码块
静态代码块指Java类中的static{}代码块,主要用于初始化类,为类的静态变量赋初始值,提示程序性能。
7.1静态代码块的特点:
1.静态代码块类似一个方法,但它不可以存在任何方法体中。
2.静态代码块可以置于类中的任何地方,类中可以有多个静态初始化块。
3.Java虚拟机在加载类时执行静态代码块,所有很多会将一些只需要进行一次的初始化操作都放在static代码块中进行。
4.如果类中包含多个静态代码块,则Java虚拟机将按它们在类中出现的顺序依次执行它们,每个静态代码块只会被执行一次。
5.静态代码块与静态方法一样,不能直接访问类的实例变量和实例方法,而需要通过类的实例对象来访问。
class StaticCode{
public static int count = 0;
{
count++;
System.out.println("非静态代码块 count = " + count);
}
static {
count++;
System.out.println("静态代码块 count = " + count);
}
static {
count++;
System.out.println("静态代码块 count = " + count);
}
}
//输出
************
执行sct1
静态代码块 count = 1
静态代码块 count = 2
非静态代码块 count = 3
执行sct2
代码示例:
/*
static关键字的使用
1.static:静态的,static修饰的成员变量和方法从属于类,而普通变量和方法从属于对象。
2.static可以用于修饰:属性,方法,代码块,内部块。
3.使用static修饰属性:静态变量(类变量)
3.1属性 按是否使用static修饰,又分为:静态属性和非静态属性(实例变量)
实例变量:指没有被static修饰,每个对象独有
静态变量:指被static修饰的成员变量,多个对象共享一个静态变量
3.2static修饰属性的其他说明:
1.静态变量随着类的加载而加载
2.静态变量的加载早于对象的创建
3.由于类自会加载一次,则静态变量在内存中也只会存在一份,存在于方法区的静态域中
4.使用static修饰方法
4.1随着类的加载而加载,可以通过类.静态方法调用
4.2静态方法中,只能调用静态的方法或属性
非静态方法中,既可以调用非静态方法或属性,也可以调用静态的方法
5.static注意点
5.1在静态方法内,不能使用this,super 关键字
5.2关于静态属性和静态方法的使用,都从生命周期的角度去理解
6.开发中,如何确定一个属性是否要声明为static?
1.统一公共属性,不随对象的不同而改变
2.final修饰的常量,通常也声明为static,静态常量,从而节省内存空间
开发中,如何确定一个属性是否要声明为static?
1.操作静态属性的方法,设置为static
2.工具类中的方法,通常设置为static(没有对象也可以使用)
7.静态代码块
静态代码块指Java类中的static{}代码块,主要用于初始化类,为类的静态变量赋初始值,提示程序性能。
静态代码块的特点:
1.静态代码块类似一个方法,但它不可以存在任何方法体中。
2.静态代码块可以置于类中的任何地方,类中可以有多个静态初始化块。
3.Java虚拟机在加载类时执行静态代码块,所有很多会将一些只需要进行一次的初始化操作都放在static代码块中进行。
4.如果类中包含多个静态代码块,则Java虚拟机将按它们在类中出现的顺序依次执行它们,每个静态代码块只会被执行一次。
5.静态代码块与静态方法一样,不能直接访问类的实例变量和实例方法,而需要通过类的实例对象来访问。
*/
public class StaticTest {
public static void main(String[] agrs){
//静态变量随着类的加载而加载
Chinese.nation = "china";
System.out.println(Chinese.nation);//china
Chinese c1 = new Chinese();
c1.name = "Yao";
c1.age = 40;
c1.nation = "CHN";
System.out.println(c1.nation);//CHN
Chinese c2 = new Chinese();
c2.name = "Long";
c2.age = 30;
System.out.println(c2.nation);//CHN
c2.nation = "CHINA";
System.out.println(c2.nation);//CHINA
System.out.println(c1.nation);//CHINA
c1.show();//I'm a Chinese, i love CHINA
//Chinese drink Chinese Baijiu.
System.out.println("******************");
c1.talk();//Chinese like eating Chinese food.
//Chinese drink Chinese Baijiu.
//Name: Yao, Age: 40, Nation: CHINA
System.out.println("***************");
Circle cir1 = new Circle();
cir1.setRadius(5.0);
System.out.println(cir1.getId());//101
System.out.println(cir1.findArea());//78.5
System.out.println(cir1.getTotal());//1
Circle cir2 = new Circle(10.0);
System.out.println(cir2.getRadius());//10.0
System.out.println(cir2.findArea());//314.0
System.out.println(cir2.getId());//102
System.out.println(cir2.getTotal());//2
//静态代码块:
System.out.println("************");
System.out.println("执行sct1");
StaticCode sct1 = new StaticCode();
System.out.println("执行sct2");
}
}
class Chinese{
String name;
int age;
static String nation;
public void eat(){
System.out.println("Chinese like eating Chinese food.");
}
public static void drink(){
System.out.println("Chinese drink Chinese Baijiu.");
}
//静态方法只能调用静态属性或方法
public static void show(){
System.out.println("I'm a Chinese, i love " + nation);
//eat();不能调用
drink();
}
//非静态方法都可以调用
public void talk(){
eat();
drink();
System.out.println("Name: " + this.name + ", Age: " + this.age + ", Nation: " + nation);
}
}
class Circle{
private double radius;
private int id;
public Circle(){
this.id = init++;
total++;
}
public Circle(double radius){
this();//代替下面注释掉两行,调用空参构造器
this.radius = radius;
// this.id = init++;
// total++;
}
private static int total;
private static int init = 101;
public double findArea(){
return 3.14 * this.radius * this.radius;
}
public void setRadius(double radius){
this.radius = radius;
}
public double getRadius(){
return this.radius;
}
public int getId(){
return this.id;
}
public static int getTotal(){
return total;
}
}
class StaticCode{
public static int count = 0;
{
count++;
System.out.println("非静态代码块 count = " + count);
}
static {
count++;
System.out.println("静态代码块 count = " + count);
}
static {
count++;
System.out.println("静态代码块 count = " + count);
}
}