final关键字简介
final是java中的一个关键字,可以用来修饰变量、方法、类,该关键字的意思跟:最后的最终的,即不可以修改的。
final修饰变量
final修饰一般变量
1 | public class Test { |
通过上述示例说明,被final修饰的变量只能赋值一次Exception in thread “main” java.lang.Error: Unresolved compilation problem:
The final field Test.b cannot be assigned
at io.github.caoxx123.extendsstudy.Test.main(Test.java:9)
final修饰对象
1 | public class Test{ |
final修饰方法
定义一个父类Base
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 public class Base {
private static final int MAX_NUM=100;
private int [] arr=new int[MAX_NUM];
private int count;
public final void add(int number) {
if(count<MAX_NUM) {
arr[count++]=number;
}
}
public void addAll(int [] numberItems) {
for(int item:numberItems) {
add(item);
}
}
}
定义一个子类,试图重写父类中被final修饰的方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package io.github.caoxx123.extendsstudy;
public class Child extends Base {
private long num;
public void add(int number) {
// TODO Auto-generated method stub
super.add(number);
}
public void addAll(int[] numberItems) {
// TODO Auto-generated method stub
for (int item : numberItems) {
add(item);
num+=item;
}
}
public long getNum() {
return num;
}
public void setNum(long num) {
this.num=num;
}
}
不能重写被final修饰的方法写一个测试类:
1
2
3
4
5
6
7
8
9
10 package io.github.caoxx123.extendsstudy;
public class Test {
public static void main(String[] args) {
Child c=new Child();
c.add(1);
}
}
Exception in thread “main” java.lang.VerifyError: class io.github.caoxx123.extendsstudy.Child overrides final method add.(I)V
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at io.github.caoxx123.extendsstudy.Test.main(Test.java:6)
final修饰类
定义一个父类Base
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 public final class Base {
private static final int MAX_NUM=100;
private int [] arr=new int[MAX_NUM];
private int count;
public final void add(int number) {
if(count<MAX_NUM) {
arr[count++]=number;
}
}
public void addAll(int [] numberItems) {
for(int item:numberItems) {
add(item);
}
}
}
定义一个子类,试图重写父类中被final修饰的方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package io.github.caoxx123.extendsstudy;
public class Child extends Base {
private long num;
public void add(int number) {
// TODO Auto-generated method stub
super.add(number);
}
public void addAll(int[] numberItems) {
// TODO Auto-generated method stub
for (int item : numberItems) {
add(item);
num+=item;
}
}
public long getNum() {
return num;
}
public void setNum(long num) {
this.num=num;
}
}
被final修饰的类不可以被继承写一个测试类:
1
2
3
4
5
6
7
8
9 package io.github.caoxx123.extendsstudy;
public class Test {
public static void main(String[] args) {
Child c=new Child();
}
}
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
The type Child cannot subclass the final class Base
at io.github.caoxx123.extendsstudy.Child.(Child.java:3)
at io.github.caoxx123.extendsstudy.Test.main(Test.java:6)