Java 最终修饰符

发布于 2024-09-29 15:20:23 字数 939 浏览 2 评论 0原文

有人告诉我,我误解了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不是你的备胎 2024-10-06 15:20:24

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.

冬天的雪花 2024-10-06 15:20:23

依次回答您的每个观点:

原始变量:只能设置一次。 (内存和性能增益)

是的,但没有内存增益,也没有性能增益。 (您所谓的性能增益仅来自设置一次......而不是来自final。)

对象变量:可以修改,final适用于对象引用。

是的。 (但是,此描述忽略了一点,即这与 Java 语言的其余部分处理对象/引用二元性的方式完全一致。例如,当对象作为参数传递并作为结果返回时。)

字段:只能设置一次。

真正的答案是:与变量相同。

方法:无法覆盖、隐藏。

是的。但还要注意,这里发生的情况是,final 关键字在不同的语法上下文中使用,其含义与字段/变量的 final 不同。

类:无法扩展。

是的。但也请参阅上面的注释。

垃圾收集:将强制Java分代垃圾收集标记-清除进行双重清除。

这是无稽之谈。 final 关键字与垃圾回收没有任何关系。您可能会将 final 与 Finalization 混淆……它们是不相关的。

但即使是终结器也不会强制进行额外的扫描。所发生的情况是,需要终结的对象被设置在一侧,直到主 GC 完成。然后 GC 在对象上运行 Finalize 方法并设置其标志......然后继续。下次 GC 运行时,该对象将被视为普通对象:

  • 如果可到达,则标记并复制;
  • 如果不可到达,则不标记。

(你的描述——“Java分代垃圾收集标记-清除”是乱码。垃圾收集器可以是“标记-清除”或“分代”(“复制”的子类)。它不能是两者。Java通常使用分代收集,只有在紧急情况下才会回退到标记清除;即当空间不足或低暂停收集器无法跟上时。)

可能会使克隆失败(这既是好事也是坏事)

我不这么认为。

可以创建不可变原语,又名 const

是的。

可以使空白不可变 - 在创建时初始化,又名只读

是的......虽然我以前从未听说过“空白不可变”这个术语。

可以使对象浅层不可变

对象的可变性是关于可观察状态是否可以改变。因此,声明属性 final 可能会也可能不会使对象表现得不可变。此外,“浅层不可变”的概念没有得到很好的定义,尤其是因为如果不深入了解类语义,就无法映射“浅层”的概念。

(需要明确的是,变量/字段的可变性是 JLS 上下文中定义明确的概念。从 JLS 的角度来看,这只是对象可变性的概念未定义。)

可以使范围/可见性不可变

术语错误。可变性与对象状态有关。可见性和范围则不然。

可以使方法调用开销更小(因为不需要虚表)

在实践中,这是无关紧要的。现代 JIT 编译器也会对非最终方法进行这种优化,前提是它们没有被应用程序实际使用的任何类覆盖。 (聪明的事情发生了......)

可以将方法参数用作最终参数(即使不是)

嗯?我无法解析这句话。

可以使对象线程安全

在某些情况下是的。

(如果对象被定义为final,它不会使方法参数成为final)

是的,如果你的意思是如果class是最终的。对象不是最终的。

可以进行模拟测试(并不是说你可以做任何事情 - 你可以说 bug 是故意的)

不解析。

无法交朋友(与其他朋友可变,休息时不可变)

Java 没有“朋友”。

无法将可变的更改为稍后更改为不可变的(但可以使用修复等工厂模式)

是的,第一个,final 字段不能从可变切换为不可变。

目前尚不清楚第二部分的意思。确实,您可以使用工厂(或构建器)模式来构造不可变对象。但是,如果您对对象字段使用final,则该对象在任何时候都不会可变。

或者,您可以实现使用非最终字段来表示不可变状态的不可变对象,并且您可以设计 API,以便您可以“翻转开关”,使以前的可变对象从现在开始变得不可变。但是,如果您采用这种方法,那么如果您的对象需要线程安全,则需要更加小心同步。

无法使数组元素不可变,即深度不可变

是的,但是你的术语被破坏了;请参阅上面关于“浅层可变性”的评论。

无法创建对象的新实例(这既好又坏)

不。没有什么可以阻止您使用 Final 字段或 Final 类或 Final 方法创建对象的新实例。

无法进行序列化

不。序列化工作。 (当然,使用自定义的 readObject 方法反序列化 final 字段会出现问题……尽管您可以使用反射黑客来解决这些问题。)

除最终版本外别无选择,

但是有包装+私有

,模数(严格来说)非final字段的非同步getter可能是非线程安全的......即使它在对象构造期间初始化并且从未改变!

和枚举。

解决了一个不同的问题。并且枚举可以是可变的。

Answering each of your points in turn:

primitive variables: can be set only once. (memory and performance gain)

Yes, but no memory gain, and no performance gain. (Your supposed performance gain comes from setting only once ... not from final.)

objects variables: may be modified, final applies to object reference.

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.)

fields: can be set only once.

The real answer is: same as for variables.

methods: can't be overridden, hidden.

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 to final for an field / variable.

classes: can't be extended.

Yes. But also see note above.

garbage collection: will force Java generational garbage collection mark-sweep to double sweep.

This is nonsense. The final keyword has no relevance whatsoever to garbage collection. You might be confusing final 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:

  • if it is reachable it is marked and copied
  • if it is not reachable it is not marked.

(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.)

Can make clone fail (this is both good and bad)

I don't think so.

Can make immutable primitives aka const

Yes.

Can make blank immutable - initialized at creation aka readonly

Yes ... though I've never heard the term "blank immutable" used before.

Can make objects shallowly immutable

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.)

Can make scope / visibility immutable

Terminology error. Mutability is about object state. Visibility and scope are not.

Can make method invocation overhead smaller (because it does not need virtual table)

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 ...)

Can make method arguments used as final (even if thy are not)

Huh? I cannot parse this sentence.

Can make objects threadsafe

In certain situations yes.

(if object is defined as final, it wont make method arguments final)

Yes, if you mean if class is final. Objects are not final.

Can make mock tests (not that you could do anything about it - you can say bugs are intended)

Doesn't parse.

Can't make friends (mutable with other friends and immutable for rest)

Java doesn't have "friends".

Can't make mutable that is changed to be immutable later (but can with factory pattern like fix)

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.

Can't make array elements immutable aka deeply immutable

Yes, but your terminology is broken; see comment above about "shallow mutability".

Can't make new instances of object (this is both good and bad)

No. There's nothing stopping you making a new instance of an object with final fields or a final class or final methods.

Can't make serialization work

No. Serialization works. (Granted, deserialization of final fields using a custom readObject method presents problems ... though you can work around them using reflection hacks.)

There are no alternatives to final,

Correct.

but there is wrapper + private

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!

and enums.

Solves a different problem. And enums can be mutable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文