非访问修饰符: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 : 对同一包内的类和所有子类可见。使用对象:变量、方法。 注意:不能修饰类(外部类)。
访问权限:
修饰符 | 当前类 | 同一包内 | 子孙类(同一包) | 子孙类(不同包) | 其他包 |
public | Y | Y | Y | Y | Y |
protected | Y | Y | Y | N | N |
default | Y | Y | Y | N | N |
private | Y | N | N | N | N |
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
}
}