Writing Final Classes and Methods

You can declare some or all of a class‘s methods final. You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses. The Objectclass does this—a number of its methods are final.

You might wish to make a method final if it has an implementation that should not be changed and it is critical to the consistent state of the object. For example, you might want to make thegetFirstPlayer method in this ChessAlgorithm class final:

class ChessAlgorithm {
    enum ChessPlayer { WHITE, BLACK }
    ...
    final ChessPlayer getFirstPlayer() {
        return ChessPlayer.WHITE;
    }
    ...
}

Methods called from constructors should generally be declared final. If a constructor calls a non-final method, a subclass may redefine that method with surprising or undesirable results.

Note that you can also declare an entire class final. A class that is declared final cannot be subclassed. This is particularly useful, for example, when creating an immutable class like theString class.

时间: 2024-10-26 23:35:40

Writing Final Classes and Methods的相关文章

Ehcache(2.9.x) - API Developer Guide, Key Classes and Methods

About the Key Classes Ehcache consists of a CacheManager, which manages logical data sets represented as Caches. A Cache object contains Elements, which are essentially name-value pairs. You can use Cache objects to hold any kind of data that you wan

java中final的意义

1.如果一个数据既是static又是final,那么它会拥有一块无法改变的存储空间. 2.final data: 当final用于基本数据类型时,final让其值(value)保持不变,但是当用于object reference时,final仅让reference保持不变.也就是说当reference一旦被初始化用于代表某个对象时,便再也不能改变指向另一个对象,但对象本身的内容确实可以改变的.final对array的作用和对reference的作用一样.参考以下例子: public class

[Java] final的意义

1.如果一个数据既是static又是final,那么它会拥有一块无法改变的存储空间. 2.final data: 当final用于基本数据类型时,final让其值(value)保持不变,但是当用于object reference时,final仅让reference保持不变.也就是说当reference一旦被初始化用于代表某个对象时,便再也不能改变指向另一个对象,但对象本身的内容确实可以改变的.final对array的作用和对reference的作用一样.参考以下例子: public class

awesome-java

Awesome Java A curated list of awesome Java frameworks, libraries and software. Awesome Java Ancients Bean Mapping Build Bytecode Manipulation Cluster Management Code Analysis Code Coverage Compiler-compiler Configuration Constraint Satisfaction Prob

Java性能提示(全)

http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLists and ArrayLists (and Vectors) (Page last updated May 2001, Added 2001-06-18, Author Jack Shirazi, Publisher OnJava). Tips: ArrayList is faster than

PowerMock简介

PowerMock 写单元测试可能有些难.有时仅仅为了可测试性的目标而不得不牺牲好的设计.通常可测试性和好的测试之间是一致的,但是并不总是如此.比如,由于现存框架的限制,final classes.methods不能被使用,private methods有时需要被保护或者没有必要移到协作者内(collaborator),static methods应当被完全避免等等. PowerMock是一个扩展了其他mock libraries(比如EasyMock)的框架,拥有更强大的功能.PowerMoc

Mock之easymock, powermock, and mockito

easymock, powermock, and mockito Easymock Class Mocking Limitations To be coherent with interface mocking, EasyMock provides a built-in behavior for equals(), toString(), hashCode() and finalize() even for class mocking. It means that you cannot reco

[转][C++ 11]override and final - write clean and maintainable C++ code

原文: http://arne-mertz.de/2015/12/modern-c-features-override-and-final/ Today I write about a pair of less often discussed, less complicated features introduced in C++11, which are nevertheless useful. Both can provide some additional security and cla

Java Modifiers, default/public/protected/private/ final/static/transient/synchronized/volatile

reference: http://www.studytonight.com/java/modifier-in-java.php Modifiers are keywords that are added to change meaning of a definition. In java, modfiers are cateogrized into two types: 1. Access control modifier 2. Non Access modifier 1) Access co