Master Do not spray ~
The final keyword can be used to decorate classes, variables, and methods, which have the meaning "This is immutable" or "final", so the final decorated classes, variables, and methods will have the following characteristics:
A 1.final decorated class cannot be inherited. Example 1
The 2.final retouching method cannot be overridden by a quilt class. Example 2
3.FIANL modified variables (member variables and local variables) are constants and can only be assigned once. Example 3
4.FIANL when modifying member variables, be sure to assign an initialization value while defining the variable. Example 4
Example 1
Final class a{//uses the final keyword to decorate Class A}class B extends a{//b class inherits Class A}class Demo1 {public static void main (string[] args) {b b = new B ();//Create an instance object of Class B}}
Operation Result:
Example 2
Class a{//defines a public final void shout () {}//uses the final keyword adornment shout () method}class B extends A{//b class inherits a class public void shout () {}// Override the Class A shout () method}class Demo1 {public static void main (string[] args) {b b = new B ();//Create an instance object of Class B}}
Operation Result:
Example 3
Class Demo1 {public static void main (string[] args) {final int x = 10;//The first time you can assign a value x=5;//again will error}}
Operation Result:
Example 4
Class a{//defines A final String name;//Use the final keyword to decorate the Name property public void introduce () {//define introduce () method, Print NameSystem.out.println ("name =" +name);}} Class Demo1 {public static void main (string[] args) {A A = new A ();//Create an instance object of Class A a.introduce ();//Call the Introduce () method of Class A)}
Operation Result:
Assigning a value to a member variable will not give an error.
Class a{//defines A type final String name = "Jianghai Hao";//Use the final keyword to decorate the Name property public void introduce () {//define introduce () method, Print NameSystem.out.println ("name =" +name);}} Class Demo1 {public static void main (string[] args) {A A = new A ();//Create an instance object of Class A a.introduce ();//Call the Introduce () method of Class A)}
Operation Result:
Exchange Learning Penguin: 654249738
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java_se base--42.final modifier