Home > Java > javaTutorial > body text

The difference between final, finally, and finalize in Java

王林
Release: 2024-02-19 12:16:22
Original
1231 people have browsed it

The difference between final, finally, and finalize in Java

The difference between final, finally, and finalize in Java requires specific code examples

In Java programming, we often encounter the three key points of final, finally, and finalize Words that, although spelled similarly, have different meanings and usage. This article will explain the differences between these three keywords in detail and give code examples to help readers better understand.

1. Final keyword
The final keyword can be used for classes, methods and variables. Its function is to make the modified class, method or variable immutable.

  1. Final class:
    The final modified class means that the class cannot be inherited, that is, its subclasses cannot exist.

Sample code:

final class A {
    // class implementation
}

// 以下代码会报错,无法继承final类A
class B extends A {
    // class implementation
}
Copy after login
  1. final method:
    The final modified method means that the method cannot be overridden by subclasses.

Sample code:

class A {
    final void foo() {
        // method implementation
    }
}

class B extends A {
    @Override
    void foo(){  //以下代码会报错,无法重写final方法
        // method implementation
    }
}
Copy after login
  1. final variable:
    The variable modified by final means that the value of the variable cannot be changed, that is, it is a constant. Final variables can be initialized when declared or in the constructor.

Sample code:

class A {
    final int x = 10;  //声明时初始化
    
    final int y;
    A(){
        y = 20;  //在构造方法中初始化
    }
}

class B {
    void foo() {
        final int z = 30;  //局部变量,必须在声明时初始化
        // method implementation
    }
}
Copy after login

2. Finally keyword
The finally keyword is often used in the try-catch-finally statement block to ensure that no matter whether an exception occurs or not, finally All code in the block will be executed. The finally block is usually used to release resources or perform some necessary cleanup work.

Sample code:

try {
    // 可能发生异常的代码
}
catch (Exception e) {
    // 异常处理逻辑
}
finally {
    // 最终执行的代码,无论是否发生异常,都会执行
    // 释放资源或者其他清理工作
}
Copy after login

3. Finalize method
The finalize() method is the garbage collection method of an object. Before the object is recycled by the garbage collector, the garbage collector will call this method. method. This method is usually used to release underlying resources related to the object. However, the finalize() method is not guaranteed to be executed in time, so it is not recommended to use this method to release important resources.

Sample code:

class A {
    // 对象的垃圾回收方法
    protected void finalize() throws Throwable {
        // 释放与对象相关的底层资源
        super.finalize();
    }
}

public static void main(String[] args) {
    A obj = new A();
    obj = null;  // 将对象置为null,让垃圾回收器回收
    System.gc();  // 手动触发垃圾回收
}
Copy after login

To sum up, the three keywords final, finally and finalize have different meanings and usages in Java. final is used to modify classes, methods and variables, indicating that they are immutable; finally is used in try-catch-finally statement blocks to ensure that the code in it will be executed regardless of whether an exception occurs; the finalize() method is the garbage collection of an object Method, called before the object is recycled. Understanding their differences and correct usage is very helpful for writing high-quality, easy-to-maintain Java code.

The above is the detailed content of The difference between final, finally, and finalize in Java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template