Detailed explanation on how to use the final keyword in java
1. Modified class
A class modified by final cannot be inherited by subclasses.
//父类Animal public final class Animal{ private int age; //年龄 private String var; //品种 public void eat(){ System.out.println("吃东西"); } } //子类cat public class cat extends Animal{ //编译时会报错,编译不通过。 public void eat(){ System.out.println("吃鱼"); } }
Free online video tutorial recommendation: java video
2. Modify member methods
Member methods modified by final cannot be overridden.
//父类Animal public class Animal{ private int age; //年龄 private String var; //品种 public final void eat(){ //成员方法 System.out.println("吃东西"); } } //子类cat public cat extends Animal{ public void eat(){ //重写父类方法。编译时会报错,编译不通过。 System.out.println("吃鱼"); } }
3. Modify basic variable types
Variables modified by final can only be assigned once.
public class Animal{ public static void main(String str){ private int i = 10; i = 20; //编译时,此处报错。 System.out.println(i); } }
4. Modified reference variables
The modified reference variable can only point to the object once.
public class Animal{ public static void main(String str){ final Cat c; c = new Cat(); c = new Cat(); } } public class Cat{ private String var; private int age; public void eat(){ System.out.println("吃鱼"); } }
5. Modified constants
格式:public static final int I = 10;
Recommended related articles and tutorials: Getting started with java development
The above is the detailed content of Detailed explanation on how to use the final keyword in java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!
