Java关键字——static、final


static关键字

static关键字,表述为“静态的,全局的”,被static修饰的资源(变量或方法),可以直接通过类名调用,而不需要实例化

1 static声明的资源只能被初始化一次,且在整个程序编译通过之后,开始运行之前完成初始化;

public class Father {
	
	public String name;
	public int age;
	
	//静态代码块
	static {
		System.out.println("ststic");
	}
	
	public Father() {//无参构造方法
		System.out.println("Father");
	}

	public static void main(String[] args) {
		Father f = new Father();
	}

}

ststic
Father

2 修饰的变量称为静态变量。局部变量(如方法中的变量)不能被static 修饰,因为static就有全局的意思;
3 修饰的方法称为静态方法,静态方法只能调用其他静态资源,不能调用非静态变量,不能应用this和super,因为静态资源的加载先于实例化

public class Father {
	
	public String name;
	public int age;
	public static String subject; //静态资源
	//静态代码块
	static {
		System.out.println("ststic");
	}
	//静态方法
	public static void learn() {
		//静态方法只能访问静态资源
		subject = "English";
	}
}

4 被static修饰的变量和方法独立于该类的任何对象。也就是说,它不依赖类特定的实例,被该类的所有实例共享

public class Father {
	
	public String name;
	public int age;
	public static String subject; //静态资源
	//静态代码块
	static {
		System.out.println("ststic");
	}
	public static void learn() {
		//静态方法只能访问静态资源
		subject = "English";
	}
	public static void main(String[] args) {
		Father f = new Father();
		f.learn();// √
		Father.learn();// √
		new Father().learn();// √
	}

}

5 修饰内部类(静态类),外部类不需要实例化,可以直接通过外部类名调用

public class Father {

	//内部类
	class Mother{

	}
	
	public static void main(String[] args) {
		Father f = new Father();
		Mother m = f.new Mother();
	}
	
}
public class Father {
	
	//内部类
	static class Mother{

	}
	
	public static void main(String[] args) {
		Mother m = new Father.Mother();
	}
	
}
  • 补充
    实例方法和所属的实例绑定
    静态方法和所属的类绑定
    静态代码块按先后顺序先于主方法执行
    注:
    静态代码块不能存在于任何方法体内
    静态代码块不能直接访问实例变量和实例方法。

请问以下代码加载顺序如何?

public class Father {
	//静态代码块
	static {
		System.out.println("Father-ststic");
	}
	//构造方法
	public Father() {
		System.out.println("Father");
	}
}

public class Child extends Father{
	//静态代码块
	static {
		System.out.println("Child-ststic");
	}
	//构造方法
	public Child() {
		System.out.println("Child");
	}
	public static void main(String[] args) {
		Child c = new Child();
	}
}

Father-ststic
Child-ststic
Father
Child

JVM的类加载顺序
static声明的静态资源 > new > {代码块} > 构造方法constructor
子父类中:父类静态资源>子类静态资源>父类代码块>父类构造方法>子类代码块>子类构造方法

public class Father {
	//静态代码块
	static {
		System.out.println("Father-ststic");
	}
	//代码块
	{
		System.out.println("Father-codeblock");
	}
	//构造方法
	public Father() {
		System.out.println("Father");
	}
}

public class Child extends Father{
	//静态代码块
	static {
		System.out.println("Child-ststic");
	}
	//代码块
	{
		System.out.println("Child-codeblock");
	}
	//构造方法
	public Child() {
		System.out.println("Child");
	}
	
	public static void main(String[] args) {
		Child c = new Child();
	}
	
}

Father-ststic
Child-ststic
Father-codeblock
Father
Child-codeblock
Child

final关键字

final关键字,表述为“最终的,不可更改的” 用来修饰类、方法和变量。

1 final修饰的类不能够被继承;
2 修饰的方法不能被重写;
3 修饰的变量为常量,常量名称必须都为大写,同时要给常量赋值;
4 修饰基本数据类型,变量的值不能被改变;
5 修饰引用类型(对象),该对象的引用(内存地址)不能被改变,即对其初始化之后便不能再让其指向另一个对象。

final int NO = 12;
final String NAME = "abc";
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值