为什么在Java中需要在Java中宣布的资源在Try-Resources声明之外宣布的资源?

发布于 2025-02-05 19:18:36 字数 170 浏览 1 评论 0原文

为什么在Java中需要在Java中宣布的资源在Try-Resources声明之外宣布的资源?例如:为什么以下代码中的Object_name需要最终或有效最终?

type object_name = new type();
try (object_name) {
  ...
}

why resources declared outside the try-with-resources statement need to be final or effectively final in java? ex: why object_name in the below code needs to be final or effectively final?

type object_name = new type();
try (object_name) {
  ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦回旧景 2025-02-12 19:18:37

这只是一个膝盖混蛋默认(一个好的):如有疑问,没有特别好的理由允许 一些奇怪的用法似乎没有多大意义,只是..不要允许它。如果事实证明不允许它丢失了很多,那么您可以稍后允许它。您不能进行反向 - 否则 稍后会向​​后不相容。

这里的特定推理很可能是这个简单的概念:

您在这里尝试什么是什么?是[a]变量,还是[b]对象?想象一下此代码:

class DummyClosable implements AutoClosable {
  String print;

  DummyClosable(String print) {
    this.print = print;
  }

  @Override public void close() {
    System.out.println(print);
  }
}

class Example {
  public static void main(String[] args) {
    DummyClosable dc = new DummyClosable("a");
    try (dc) {
      dc = new DummyClosable("b");
    }
  }
}

它是打印“ A”或“ B”吗?我可以看到某人完全说服它应该打印“ A”,我可以看到某人是如何完全说服它应该印刷的“ B”。

但是,我特别可以看到绝大多数Java程序员将如何迅速弄清楚它会打印出一种或另一个,但不确定它是哪一个。因为试图自动关闭非决赛极为罕见,所以他们不会有经验,因此大多会诅咒代码:他们现在需要猜测它的作用,或者试图用相反的上下文弄清楚它严重的惩罚使这会使任何错误在此操作中持续下去,或者实验/读取需要时间的文档。

It's just a knee jerk default (a good one): When in doubt and there is no particularly good reason to allow some weird usage that doesn't appear to make much sense, just.. don't allow it. If it turns out that a lot is lost by not allowing it, you can simply allow it later. You can't do the reverse - disallowing it later would be backwards incompatible.

The specific reasoning here is most likely this simple concept:

What is the thing you are 'try-with'ing here? Is it [A] the variable, or [B] the object? Imagine this code:

class DummyClosable implements AutoClosable {
  String print;

  DummyClosable(String print) {
    this.print = print;
  }

  @Override public void close() {
    System.out.println(print);
  }
}

class Example {
  public static void main(String[] args) {
    DummyClosable dc = new DummyClosable("a");
    try (dc) {
      dc = new DummyClosable("b");
    }
  }
}

Does that print 'a', or 'b'? I can see how someone is utterly convinced it should print 'a', and I can see how someone is utterly convinced it should print 'b'.

But I can especially see how the vast majority of java programmers will quickly figure out it will print one or the other, but aren't sure which one it is. Because trying to auto-close non-finals is extremely rare, they won't have the experience and thus will mostly just curse the code: They now need to either guess what it does, or try to figure it out from context with the rather drastic penalty that this perpetuates any bugs in what this does, or experiment/read docs which takes time.

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