Java final modifier: final modifier attribute, final modifier method and final modifier class

1. Final modifies the attributes in the class

2. Methods in final modifier class

3. Final modifier class

Example 1

public class FinalTest {
    final int count = 1;
    public int updateCount() {
        count = 4;    // 修改final属性值,提示错误
        return count;
    }
    public final int sum() {
        int number = count+10;
        return number;
    }
}
public class FinalExtendTest extends FinalTest {
    public int sum(){};    // 重写父类 FinalTest 中的 sum()方法,出错
    int count = sum();    // 继承父类 FinalTest 中的 sum()方法
}
public static void main(String[] args) {
    FinalExtendTest fet = new FinalExtendtest();
    System.out.println(fet.count);
}
11
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>