java 为什么Java中没有局部静态变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12274375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:17:11  来源:igfitidea点击:

Why there is no local static variable in Java?

javaoopstatic

提问by Rafi Kamal

In C/C++ we use static local variables for maintaining a method's state. But why it is not supported in Java?

在 C/C++ 中,我们使用静态局部变量来维护方法的状态。但是为什么它在 Java 中不受支持?

Yes, I can use an static field for this purpose. But isn't it a bit weird to create a field for maintaining only one method's state?

是的,我可以为此使用静态字段。但是创建一个字段来只维护一个方法的状态是不是有点奇怪?

回答by Tony K.

You have found the only solution.

您已经找到了唯一的解决方案。

Java dropped a number of complexities from C++, and this was one of them.

Java 从 C++ 中删除了许多复杂性,这就是其中之一。

Static variables scoped to a function do nasty things to you in concurrency (e.g. strtok is a famously nasty one to use with pthreads, for exactly this reason).

作用域为函数的静态变量在并发中对您做一些讨厌的事情(例如,strtok 是众所周知的与 pthreads 一起使用的讨厌的变量,正是出于这个原因)。

In general, what you want is an object with state. The function in question should then have an object-level variable. Then you can create instances that each maintain state.

一般来说,你想要的是一个有状态的对象。有问题的函数应该有一个对象级变量。然后您可以创建每个维护状态的实例。

Much easier to understand/maintain/etc.

更容易理解/维护/等。

If you truly need to maintain state as a singleton, then static fields are it.

如果你真的需要将状态作为单例来维护,那么静态字段就是它。

回答by GargantuChet

The Java language spec doesn't seem to defend the omission of variables that correspond to C staticvariables.

Java 语言规范似乎并没有为省略与 Cstatic变量对应的变量辩护。

Hiding state in class methods has a few drawbacks when seen from a Java perspective. Generally the existence of a function-level static variable isn't the sort of implementation detail that you'd want to expose outside of that function.

从 Java 的角度来看,在类方法中隐藏状态有一些缺点。通常,函数级静态变量的存在并不是您希望在该函数之外公开的那种实现细节。

But the method's state is actually part of the class's state, and method-level static variables would have to be serialized / deserialized any time the object is persisted. This might not sound common, coming from a C background, so I'll note a few common examples.

但是方法的状态实际上是类状态的一部分,并且只要对象被持久化,方法级的静态变量就必须被序列化/反序列化。这可能听起来不常见,来自 C 背景,所以我会注意一些常见的例子。

  • Application server clusters can pass user session objects between nodes in order to provide fault tolerance.
  • JAXB could be used to marshall an object into an XML document
  • JPA can be used to persist object state to a database
  • 应用服务器集群可以在节点之间传递用户会话对象以提供容错能力。
  • JAXB 可用于将对象编组到 XML 文档中
  • JPA 可用于将对象状态持久化到数据库

If the variable's value isworth saving when the object is persisted, then there's a good chance that code outside of that class will need to reference that value. And suddenly that means defining access levels -- is a static variable in a public method automatically public? Or would a programmer have to declare it so?

如果变量的值当对象被持久值得保存,然后有一个很好的机会,这个类的代码之外需要引用该值。突然间,这意味着定义访问级别——公共方法中的静态变量是否自动公开?或者程序员必须这样声明吗?

We also have to think about extensibility. Would derived classes be required to implement the same static variable? Or would there be a reference to the variable from the function in the base class?

我们还必须考虑可扩展性。派生类是否需要实现相同的静态变量?或者是否会引用基类中的函数中的变量?

It's more likely that the C method that would use a static local variable would be a good candidate for a class in Java. It has state and hopefully exists for a single purpose. There's little drawback to encapsulating the functionality into an object, and it makes for a cleaner separation between transient values (such as local variables) and more long-term state.

使用静态局部变量的 C 方法更有可能成为 Java 类的良好候选者。它有状态,并希望为单一目的而存在。将功能封装到一个对象中几乎没有什么缺点,它可以在瞬态值(例如局部变量)和更长期的状态之间进行更清晰的分离。

回答by Thirler

Some of the other answers show why you might not want to have this. But you can also ask why from a historical perspective.

其他一些答案显示了为什么您可能不想拥有它。但是你也可以从历史的角度来问为什么。

To answer this you have to start to see why C does have static local variables. C has much fewer means than Java and C++ to limit the scope of a variable, the only options for static data are 'inside the file' and 'everywhere'. So this provides an extra layer, to limit the scope.

要回答这个问题,您必须开始了解为什么 C 确实有静态局部变量。C 比 Java 和 C++ 限制变量范围的方法少得多,静态数据的唯一选项是“在文件内”和“无处不在”。所以这提供了一个额外的层,以限制范围。

An important aspect of C++ is compatibility with, so it is allowed in C++ as well. But it doesn't need local static scope as much anymore, because there are many other means to limit scope of static data. The use is not popular in (modern) C++.

C++ 的一个重要方面是兼容性,因此在 C++ 中也是允许的。但是它不再需要本地静态范围,因为还有许多其他方法可以限制静态数据的范围。这种用法在(现代)C++ 中并不流行。

Java merely takes a lot of inspiration from C/C++, it didn't have to worry about backwards compatibility, so it could be left out.

Java 只是从 C/C++ 中汲取了很多灵感,它不必担心向后兼容性,因此可以将其排除在外。

回答by Marc Polizzi

Perhaps because methods are not objects in Java; so maintaining their state as you said make not much sense and I guess you'd have to create a new concept in the byte code for that; use an object as Tony K. said.

也许是因为方法不是 Java 中的对象;所以像你说的那样保持它们的状态没有多大意义,我想你必须为此在字节码中创建一个新概念;像 Tony K. 所说的那样使用一个对象。

回答by Java Tutorial

instance methods are invoked by the instance(objects) of the class . Static things belongs to the class not to the object that's why local variables are not static.Instance variables are static and they can also initialized at the time of class loading by static blocks. enter image description here

实例方法由类的实例(对象)调用。静态的东西属于类而不属于对象,这就是为什么局部变量不是静态的。实例变量是静态的,它们也可以在类加载时由静态块初始化。 在此处输入图片说明

for more information please visit :- https://www.youtube.com/watch?v=GGay1K5-Kcs&t=119s

欲了解更多信息,请访问:- https://www.youtube.com/watch?v=GGay1K5-Kcs&t=119s