final,static,其他访问修饰词,静态变量和实例变量的区别

非访问修饰符:static、final、abstract、synchronized、volatile
static:用来修饰类方法和类变量
特点:1、可以修饰成员变量,成员方法

class TestStatic {
	// 修饰成员方法
	static int sum = 20;
	// 修饰成员变量
	static void show() {
		System.out.println("可以直接被类名调用");
	}
}

2、随着类的加载而加载,优先于对象加载

// 测试加载顺序
public class StaticUpload extends UploadTest {
	public StaticUpload() {
		System.out.println("我是最后一个被加载的");// 10.被加载
	}
	static int num = 3;// 3.被加载
	static {
		System.out.println("我是第四个被加载的,我前面还有:" + num); // 4.被加载
	}
	int numre = 8; // 8.被加载
	{
		System.out.println("我是第九个被加载的,我前面还有:" + numre);// 9.被加载
	}
	public static void main(String[] args) {
		StaticUpload ht = new StaticUpload();
	}
}
class UploadTest {
	static int num = 1;// 1.首先被加载
	static {
		System.out.println("我是第二个被加载的,我前面还有:" + num); // 2.被加载
	}
	int count = 5; // 5.被加载
	{
		System.out.println("我是第六个被加载的,我前面还有:" + count);// 6.被加载
	}
	public UploadTest() {
		System.out.println("我是第七个被加载的:");// 7.被加载
	}
}

3、只加载一次,就会一直存在,不再开辟新空间

// 测试只加载一次
public class StaticTest {
	private static int staticInt = 2;
	private int random = 2;
	public StaticTest() {
		staticInt++;
		random++;
		System.out.println("staticInt = " + staticInt + "  random = " + random);
	}
	public static void main(String[] args) {
		StaticTest test1 = new StaticTest(); // staticInt = 3 random = 3
		StaticTest test2 = new StaticTest(); // staticInt = 4 random = 3
		StaticTest test3 = new StaticTest(); // staticInt = 5 random = 3
	}
}

4、全局共享,可以被多个对象共享

// 测试全局共享
public class Test {
	public static void main(String[] args) {
		StaticTest.share2(); // 2
		StaticTest.share1(); // 10
		StaticTest.share2(); // 10
	}
}
class StaticTest {
	private static int staticInt = 2;
	public static void share1() {
		staticInt = 10;
		System.out.println("share1:" + staticInt);
	}
	
	public static void share2() {
		System.out.println("share2:" + staticInt);
	}
}

5、可以直接被类名调用

// 测试可以直接被类名调用
class TestStatic {
	static int sum = 20;
	static void show() {
		System.out.println("可以直接被类名调用");
	}
}
public class StaticTest {
	public static void main(String[] args) {
		TestStatic.show();
		int sum = TestStatic.sum;
		System.out.println(sum);
	}
}

6、静态只能调用静态,非静态可以随意调用

// 测试静态只能调用静态,非静态可以随意调用
public class StaticTest {
	private static int staticInt = 2;
	private int random = 2;
	public static void only1() {
		staticInt++;
		random++; // 报错(Cannot make a static reference to the non-static field random|不能对非静态字段random进行静态引用)
	}
	public void only2() {
		staticInt++;
		random++;
	}
}

7、static不能和this或者super共用,因为有static时可能还没有对象

// static不能和this或者super共用,因为有static时可能还没有对象
public class StaticTest {
	public void thisThis() {
		System.out.println("静态方法不能使用this关键字和super关键字");
	}
	public void this1() {
		this.thisThis();
	}
	public static void this2() {
		this.thisThis(); // 报错(Cannot use this in a static context|不能在静态上下文中使用这个)
	}
}

final:用来修饰类、方法和变量,final修饰的类不能够被继承,修饰的方法不能被继承类重新定义,修饰的变量为常量,是不可修改的.
特点:1、被final修饰的类,不能被继承

// 测试被final修饰的类,不能被继承
public class FinalTest extends NoExFinal {}
final class NoExFinal {}
class ExFinal {}

2、被final修饰的方法,不能被重写

// 测试被final修饰的方法,不能被重写
public class FinalTest extends ExFinal {
	public void show1() {
		System.out.println("final修饰的方法不能被重写");
	}
	public void show2() {
		System.out.println("没有final修饰的方法可以被重写");
	}
}
class ExFinal {
	public final void show1() {
		System.out.println("final修饰的方法不能被重写");
	}
	public void show2() {
		System.out.println("没有final修饰的方法可以被重写");
	}
}

3、被final修饰的变量是个常量,值不能被更改

// 测试被final修饰的变量是个常量,值不能被更改
public class FinalTest {
	final String NAME = "小六子";
	String name = "王五";
	public void show() {
		NAME = "张三"; // 报错(The final field FinalTest.NAME cannot be assigned|最后的测试,无法分配名称)
		name = "赵四";
	}
}

abstract:用来创建抽象类和抽象方法
synchroized:修饰方法,同一时间只能被一个线程访问
volatile:用于线程的编程

访问修饰符:default、private、public、protected
default (即默认,什么也不写): 在同一包内可见,不使用任何修饰符。使用对象:类、接口、变量、方法。
private : 在同一类内可见。使用对象:变量、方法。 注意:不能修饰类(外部类)
public : 对所有类可见。使用对象:类、接口、变量、方法
protected : 对同一包内的类和所有子类可见。使用对象:变量、方法。 注意:不能修饰类(外部类)。
访问权限:

修饰符当前类同一包内子孙类(同一包)子孙类(不同包)其他包
publicYYYYY
protectedYYYNN
defaultYYYNN
privateYNNNN
静态变量和实例变量的区别 Java类的成员变量有2种:一种是被static关键字修饰的变量,叫类变量或者静态变量;另一种没有static修饰,为实例变量,又叫 成员变量、对象变量。 区别: 1.存放位置不同 类变量随着类的加载存在于方法区中,实例变量随着对象的建立存在于堆内存中。 2.生命周期不同 静态变量的生命周期最长,随着类的加载而加载,随着类的消失而消失, 实例变量随着对象的消失而消失。
public class StaticTest {
	private static int staticInt = 2;// 静态变量
	private int random = 2;// 实例变量
	public StaticTest() {
		staticInt++;
		random++;
		System.out.println("staticInt = " + staticInt + "  random = " + random);
	}
	public static void main(String[] args) {
		StaticTest test1 = new StaticTest(); // staticInt = 3 random =3
		StaticTest test2 = new StaticTest(); // staticInt = 4 random =3
		StaticTest test3 = new StaticTest(); // staticInt = 5 random =3
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值