关于java:何时在课前使用“final”关键字?

When to use “final” keyword before a class?

本问题已经有最佳答案,请猛点这里访问。

我知道,当最后关键词在一个班之前被使用时,这个班不会被另一个班遗传下来。

但我从来没有看到它在日本编码中的实际用途,除了不可撤销的类别。

在什么场景下,它真正需要在一个班之前使用最后关键词?

难道这并不能减少爪哇语言的可靠性吗?


无法继承最终类。因此,如果您希望没有人可以继承您的类,那么您可以将其声明为final。所以你已经回答了你自己的问题。所以主要用途是

  • 不可变类型
  • 如果你不想有人延长课程。
  • 它们都是出于安全考虑而使用的。通过使用关键类来保护要更改的系统。这不足以作为一个理由吗?


    final类不能是子类。这样做是为了安全和效率。Java API中的一些类是EDCOX1×0,例如JavaLang.Stand。有时,安全性和不变性远比可重用性重要。

    根据这篇IBM developerWorks文章:

    The common perception is that declaring classes or methods final makes it easier for the compiler to inline method calls, but this perception is incorrect (or at the very least, greatly overstated).

    final classes and methods can be a significant inconvenience when programming -- they limit your options for reusing existing code and extending the functionality of existing classes. While sometimes a class is made final for a good reason, such as to enforce immutability, the benefits of using final should outweigh the inconvenience. Performance enhancement is almost always a bad reason to compromise good object-oriented design principles, and when the performance enhancement is small or nonexistent, this is a bad trade-off indeed.

    另请阅读此打开-关闭原则:

    Software Entities (Classes, Modules, Functions, etc.) should be open for Extension, but closed for Modification.


    final关键字可以与类一起使用,以提供安全性。我们可以以字符串为例。String类是不可变的,也是最终的,以增强Java中文件处理的安全性。

    Though, performance is also a reason (assuming you are already aware of the internal String pool maintained for making sure that the same String object is used more than once without having to create/re-claim it those many times), but the main reason why String has been made immutable in Java is 'Security'. Surprised? Let's understand why.

    Suppose you need to open a secure file which requires the users to authenticate themselves. Let's say there are two users named 'user1' and 'user2' and they have their own password files 'password1' and 'password2', respectively. Obviously 'user2' should not have access to 'password1' file.

    As we know the filenames in Java are specified by using Strings. Even if you create a 'File' object, you pass the name of the file as a String only and that String is maintained inside the File object as one of its members.

    Had String been mutable, 'user1' could have logged into using his credentials and then somehow could have managed to change the name of his password filename (a String object) from 'password1' to 'password2' before JVM actually places the native OS system call to open the file. This would have allowed 'user1' to open user2's password file. Understandably it would have resulted into a big security flaw in Java. I understand there are so many 'could have's here, but you would certainly agree that it would have opened a door to allow developers messing up the security of many resources either intentionally or un-intentionally.

    With Strings being immutable, JVM can be sure that the filename instance member of the corresponding File object would keep pointing to same unchanged"filename" String object. The 'filename' instance member being a 'final' in the File class can anyway not be modified to point to any other String object specifying any other file than the intended one (i.e., the one which was used to create the File object).

    更多信息可在这里找到源A来源B


    我研究了这一点,我读到了关于硬核Java,出版商:O'ReLyISBN:0596-0568-7

    为什么类被标记为最终类:

    有两种方法可以让一个班级进入决赛。第一种方法是在类声明中使用关键字final:

    1
    2
    3
    public final class SomeClass {
      //  . . . Class contents
    }

    使类成为最终类的第二种方法是将其所有构造函数声明为私有的:

    1
    2
    3
    4
    public class SomeClass {
      public final static SOME_INSTANCE = new SomeClass(5);
      private SomeClass(final int value) {
      }

    把它标记为final,如果发现它是实际的final,就可以省去麻烦,来演示一下这个测试类。乍一看就公之于众。

    1
    2
    3
    4
    5
    6
    public class Test{
      private Test(Class beanClass, Class stopClass, int flags)
        throws Exception{
        //  . . . snip . . .
      }
    }

    不幸的是,由于类的唯一构造函数是私有的,所以不可能扩展这个类。对于测试类,没有理由认为类应该是最终的。测试类是一个很好的例子,说明隐式最终类如何引起问题。

    因此,当通过使类的构造函数成为私有类而隐式地使其成为最终类时,应该将其标记为final。


    final类不能是子类。这对于提高安全性是必要的,即使它有一些缺点。

    例如,java.lang.String类是final类。因此,您不能将String子类化,并且可以确保String参数绝不是有害的子类(例如,将String发送到某处)。