Java18: Modifier words

Source: Internet
Author: User
Tags modifier

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

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.