Final
Final last
The final decorated class cannot be inherited
Infrequently used, often used for Java systems
Package day05;//System Final class//:string Math Integer longpublic class Demo04 {public static void main (string[] args) {}}final Class Dog{}class Pup extends dog{//compilation error, final class cannot be inherited//cannot be inherited, cannot modify parent class}
Final method
The final method cannot be overridden
Package day05;//System Final class//:string Math Integer longpublic class Demo04 {public static void main (string[] args) {}}final Class Dog {}class Pup extends Dog {//Compile error, final class cannot be inherited//cannot be inherited, cannot modify the method of the}class Animal {final void print () {};} Class Cat extends Animal {void print () {//Compile error, cannot override final method};}
Final variable
Only initialization (first assignment) can no longer be modified.
The final class of the package day05;//system//:string Math Integer Longpublic class Demo04 {public static void main (string[] args) {int n; f inal int m;n = 10;m =10;//final Variable first assignment (initialization) n =11;m = 11;//compilation error, final variable cannot be modified}}final class Dog {}class Pup extends Dog {// Compilation error, the final class cannot be inherited//cannot be inherited, cannot modify the method of the class}class Animal {final void print () {};} Class Cat extends Animal {void print () {//Compile error, cannot override final method};}
Final method parameters
The final class of the package day05;//system//:string Math Integer Longpublic class Demo04 {public static void main (string[] args) {int n; f inal int m;n = 10;m =10;//final Variable first assignment (initialization) n =11;m = 11;//compilation error, final variable cannot modify test (13,13);//method parameter initialization}public static void Test ( int n,final int m) {n = 12;m = 12;//Compile error, method is initialized when parameter is passed, the final variable cannot be modified in this case}}final class Dog {}class Pup extends Dog {// The final class cannot be inherited//cannot be inherited and cannot modify the method of the class}class Animal {final void print () {};} Class Cat extends Animal {void print () {//Compile error, cannot override final method};}
Sex, * * * Birth date can not change is final
Static final one can not change is constant is fixed not become
Package Day05;public class Demo06 {public static final double PI = 3.1415926;////Java Constants Use capital letters//constant pi//literal 3.1515926pub Lic static void Main (string[] args) {}}
This article is from the "Romantic Laugh" blog, make sure to keep this source http://lmdtx.blog.51cto.com/6942028/1700211
Java18: Modifiers