Java 最终修饰符
有人告诉我,我误解了 final
的效果。 final
关键字有什么作用?
以下是我的想法的简短概述,我知道:
Java Final 修饰符(又名聚合关系)
原始变量:只能设置一次。 (内存和性能 增益)
对象变量:可以修改,final适用于对象 参考。
字段:只能设置一次。
方法:无法覆盖、隐藏。
类:无法扩展。
垃圾回收:将强制Java分代垃圾回收 标记-扫描到双重扫描。
Can 和 Cant
- 可以使克隆失败(这既是好也是坏)
- 可以使不可变原语又名 const
- 可以使空白不可变 - 在创建时初始化又称为只读
- 可以使对象浅层不可变
- 可以使作用域/可见性不可变
- 可以使方法调用开销更小(因为它不需要虚表)
- 可以使方法参数用作最终(即使不是)
- 可以使对象线程安全(如果对象被定义为最终,它不会使方法参数成为最终)
- 可以进行模拟测试(不是你可以做的 )关于它的任何事情 - 你可以说错误是故意的)
- 不能交朋友(与其他朋友可变并且休息时不可变)
- 不能使可变的稍后更改为不可变(但可以使用像修复这样的工厂模式)
- 不能使数组元素不可变,又名深度不可变
- 无法创建对象的新实例(这既好又坏)
- 无法使序列化工作
除了 final
之外没有其他选择,但有包装器 + private 和枚举。
I was told that, I misunderstand effects of final
. What are the effects of final
keyword?
Here is short overview of what I think, I know:
Java final modifier (aka aggregation relation)
primitive variables: can be set only once. (memory and performance
gain)
objects variables: may be modified, final applies to object
reference.
fields: can be set only once.
methods: can't be overridden, hidden.
classes: can't be extended.
garbage collection: will force Java generational garbage collection
mark-sweep to double sweep.
Can's and Cant's
- Can make clone fail (this is both good and bad)
- Can make immutable primitives aka const
- Can make blank immutable - initialized at creation aka readonly
- Can make objects shallowly immutable
- Can make scope / visibility immutable
- Can make method invocation overhead smaller (because it does not need virtual table)
- Can make method arguments used as final (even if thy are not)
- Can make objects threadsafe (if object is defined as final, it wont make method arguments final)
- Can make mock tests (not that you could do anything about it - you can say bugs are intended)
- Can't make friends (mutable with other friends and immutable for rest)
- Can't make mutable that is changed to be immutable later (but can with factory pattern like fix)
- Can't make array elements immutable aka deeply immutable
- Can't make new instances of object (this is both good and bad)
- Can't make serialization work
There are no alternatives to final
, but there is wrapper + private and enums.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Final 关键字通常用于保持不变性。对类或方法使用final是为了防止方法之间的联系被破坏。例如,假设类 X 的某个方法的实现假定方法 M 将以某种方式运行。将 X 或 M 声明为 Final 将防止派生类重新定义 M 从而导致 X 行为不正确。
Final keyword is usually used to preserve immutability. To use final for classes or methods is to prevent linkages between methods from being broken. For example, suppose the implementation of some method of class X assumes that method M will behave in a certain way. Declaring X or M as final will prevent derived classes from redefining M in such a way as to cause X to behave incorrectly.
依次回答您的每个观点:
是的,但没有内存增益,也没有性能增益。 (您所谓的性能增益仅来自设置一次......而不是来自
final
。)是的。 (但是,此描述忽略了一点,即这与 Java 语言的其余部分处理对象/引用二元性的方式完全一致。例如,当对象作为参数传递并作为结果返回时。)
真正的答案是:与变量相同。
是的。但还要注意,这里发生的情况是,
final
关键字在不同的语法上下文中使用,其含义与字段/变量的final
不同。是的。但也请参阅上面的注释。
这是无稽之谈。
final
关键字与垃圾回收没有任何关系。您可能会将final
与 Finalization 混淆……它们是不相关的。但即使是终结器也不会强制进行额外的扫描。所发生的情况是,需要终结的对象被设置在一侧,直到主 GC 完成。然后 GC 在对象上运行 Finalize 方法并设置其标志......然后继续。下次 GC 运行时,该对象将被视为普通对象:
(你的描述——“Java分代垃圾收集标记-清除”是乱码。垃圾收集器可以是“标记-清除”或“分代”(“复制”的子类)。它不能是两者。Java通常使用分代收集,只有在紧急情况下才会回退到标记清除;即当空间不足或低暂停收集器无法跟上时。)
我不这么认为。
是的。
是的......虽然我以前从未听说过“空白不可变”这个术语。
对象的可变性是关于可观察状态是否可以改变。因此,声明属性
final
可能会也可能不会使对象表现得不可变。此外,“浅层不可变”的概念没有得到很好的定义,尤其是因为如果不深入了解类语义,就无法映射“浅层”的概念。(需要明确的是,变量/字段的可变性是 JLS 上下文中定义明确的概念。从 JLS 的角度来看,这只是对象可变性的概念未定义。)
术语错误。可变性与对象状态有关。可见性和范围则不然。
在实践中,这是无关紧要的。现代 JIT 编译器也会对非最终方法进行这种优化,前提是它们没有被应用程序实际使用的任何类覆盖。 (聪明的事情发生了......)
嗯?我无法解析这句话。
在某些情况下是的。
是的,如果你的意思是如果class是最终的。对象不是最终的。
不解析。
Java 没有“朋友”。
是的,第一个,
final
字段不能从可变切换为不可变。目前尚不清楚第二部分的意思。确实,您可以使用工厂(或构建器)模式来构造不可变对象。但是,如果您对对象字段使用
final
,则该对象在任何时候都不会可变。或者,您可以实现使用非最终字段来表示不可变状态的不可变对象,并且您可以设计 API,以便您可以“翻转开关”,使以前的可变对象从现在开始变得不可变。但是,如果您采用这种方法,那么如果您的对象需要线程安全,则需要更加小心同步。
是的,但是你的术语被破坏了;请参阅上面关于“浅层可变性”的评论。
不。没有什么可以阻止您使用 Final 字段或 Final 类或 Final 方法创建对象的新实例。
不。序列化工作。 (当然,使用自定义的
readObject
方法反序列化final
字段会出现问题……尽管您可以使用反射黑客来解决这些问题。)。
,模数(严格来说)非final字段的非同步getter可能是非线程安全的......即使它在对象构造期间初始化并且从未改变!
解决了一个不同的问题。并且
枚举
可以是可变的。Answering each of your points in turn:
Yes, but no memory gain, and no performance gain. (Your supposed performance gain comes from setting only once ... not from
final
.)Yes. (However, this description miss the point that this is entirely consistent with the way that the rest of the Java language deals with the object / reference duality. For instance, when objects are passed as parameters and returned as results.)
The real answer is: same as for variables.
Yes. But also note that what is going on here is that the
final
keyword is being used in a different syntactic context to mean something different tofinal
for an field / variable.Yes. But also see note above.
This is nonsense. The
final
keyword has no relevance whatsoever to garbage collection. You might be confusingfinal
with finalization ... they are unrelated.But even finalizers don't force an extra sweep. What happens is that an object that needs finalization is set on one side until the main GC finishes. The GC then runs the finalize method on the object and sets its flag ... and continues. The next time the GC runs, the object is treated as a normal object:
(Your characterization - "Java generational garbage collection mark-sweep" is garbled. A garbage collector can be either "mark-sweep" OR "generational" (a subclass of "copying"). It can't be both. Java normally uses generational collection, and only falls back to mark-sweep in emergencies; i.e. when running out of space or when a low pause collector cannot keep up.)
I don't think so.
Yes.
Yes ... though I've never heard the term "blank immutable" used before.
Object mutability is about whether observable state may change. As such, declaring attributes
final
may or may not make the object behave as immutable. Besides the notion of "shallowly immutable" is not well defined, not least because the notion of what "shallow" is cannot be mapped without deep knowledge of the class semantics.(To be clear, the mutability of variables / fields is a well defined concept in the context of the JLS. It is just the concept of mutability of objects that is undefined from the perspective of the JLS.)
Terminology error. Mutability is about object state. Visibility and scope are not.
In practice, this is irrelevant. A modern JIT compiler does this optimization for non-final methods too, if they are not overridden by any class that the application actually uses. (Clever stuff happens ...)
Huh? I cannot parse this sentence.
In certain situations yes.
Yes, if you mean if class is final. Objects are not final.
Doesn't parse.
Java doesn't have "friends".
Yes to the first, a
final
field can't be switched from mutable to immutable.It is unclear what you mean by the second part. It is true that you can use a factory (or builder) pattern to construct immutable objects. However, if you use
final
for the object fields at no point will the object be mutable.Alternatively, you can implement immutable objects that use non-final fields to represent immutable state, and you can design the API so that you can "flip a switch" to make a previously mutable object immutable from now onwards. But if you take this approach, you need to be a lot more careful with synchronization ... if your objects need to be thread-safe.
Yes, but your terminology is broken; see comment above about "shallow mutability".
No. There's nothing stopping you making a new instance of an object with final fields or a final class or final methods.
No. Serialization works. (Granted, deserialization of
final
fields using a customreadObject
method presents problems ... though you can work around them using reflection hacks.)Correct.
Yes, modulo that (strictly speaking) an unsynchronized getter for a non-final field may be non-thread-safe ... even if it is initialized during object construction and then never changed!
Solves a different problem. And
enums
can be mutable.