浅谈final关键字

final关键字简介

final是java中的一个关键字,可以用来修饰变量、方法、类,该关键字的意思跟:最后的最终的,即不可以修改的。

final修饰变量

final修饰一般变量

1
2
3
4
5
6
7
8
9
10
public class Test {
public final int b=100;


public static void main(String[] args) {
Test test=new Test();
test.b=10000;
}

}

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修饰的变量只能赋值一次

final修饰对象

1
2
3
4
5
6
7
public class Test{
public static void main(String [] args){
final StringBuffer stringBuffer=new StringBuffer();
stringBuffer=new StringBuffer("234");//报错
stringBuffer.append("hello world");//可以执行
}
}
被final修饰的对象不可以改变,但是对象的内容可以改变。通俗点讲就是门牌号不可以变,但是对应的门牌号里住的人可以改变

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;

@Override
public void add(int number) {
// TODO Auto-generated method stub
super.add(number);
}

@Override
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;
}
}

写一个测试类:

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修饰的方法

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;

@Override
public void add(int number) {
// TODO Auto-generated method stub
super.add(number);
}

@Override
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;
}
}

写一个测试类:

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)

被final修饰的类不可以被继承
文章作者: Anders Cao
文章链接: http://yoursite.com/2019/03/10/浅谈final关键字/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Anders's Blog
打赏
  • 微信
  • 支付寶