Java_se base--42.final modifier

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.