简体   繁体   English

我应该避免使用 STATIC 变量吗

[英]Should i avoid using STATIC vaiables

I am designing a java API(not an API exactly) in my office which will contain 4000+ constants.我正在我的办公室中设计一个 java API(不是 API),其中包含 4000+ 个常量。 So all the teams can use them directly.所以所有的团队都可以直接使用它们。 Initially i thought to create their classes according to their type and create their static objects into a separate class.最初我想根据他们的类型创建他们的类,并将他们的 static 对象创建到一个单独的 class 中。 So anybody any use them directly.所以任何人都可以直接使用它们。

But after reading need of static variable , i afraid it could be a problem of creating so many static variables.但是在阅读了 static 变量的需要之后,我担心创建这么多 static 变量可能是个问题。 Is there any alternate?有替代品吗?

*After me whoever will join this project can also add a constraint in my Constant class without caring of performance. *在我之后加入这个项目的人也可以在我的常量 class 中添加一个约束,而不关心性能。 There is a possibility that many of the constants will be used rarely.有可能很少使用许多常量。

*Every member of Constant class will represent a class which will have its own behavior. *常量 class 的每个成员都将代表一个 class ,它会有自己的行为。 It might be the part of some inheritance tree further.它可能是一些 inheritance 树的一部分。 So using enum might not be a good idea.所以使用枚举可能不是一个好主意。

You want to create a location where some 4000+ constants will live.您想创建一个可以容纳 4000 多个常量的位置。 there's the possibility that users of this class may add constants (possibly at runtime)?.这个 class 的用户有可能添加常量(可能在运行时)? T

  • Concern about the memory issues of statics is misplaced.对 memory 静力学问题的担忧是错误的。 If you need 4000 values, they're going to have to live somewhere, right?如果您需要 4000 个值,它们将不得不存在于某个地方,对吗?

  • If people will be adding values at runtime, this sounds like a singleton Map or Properties (which is really just a kind of map anyway) of some kinds.如果人们将在运行时添加值,这听起来像是某种 singleton Map 或属性(这实际上只是一种 map)。 People often use dependency injection frameworks like Spring or Guice to manage this sort of thing.人们经常使用像 Spring 或 Guice 这样的依赖注入框架来管理这类事情。

  • If you just mean adding compile constants, you can make them all static.如果您只是想添加编译常量,您可以将它们全部设为 static。 You'd probably want to make them static final as well, they'll be compiled inline.您可能还想让它们 static 最终化,它们将被内联编译。

  • It's very likely that 4000 constants is a very bad idea. 4000 个常量很可能是一个非常糟糕的主意。 Where I've seen systems with large numbers of constants (>100, even) defined in one place, what usually happens is that people forget the definitions of them and end up using their own variants, which sort of defeats the purpose (for example, I've worked on a system with 100's of SQL queries defined in a "Queries" class. Of course people immediately ignore it as it's more of a bother to look up if the exact query you need is in there than to roll your own. The class eventually grew to something like 1500 queries, many exact duplicates and many unused, most used once. Utterly pointless).我见过在一个地方定义了大量常量(甚至超过 100 个)的系统,通常发生的情况是人们忘记了它们的定义并最终使用自己的变体,这有点违背了目的(例如, 我在一个系统上工作过,在“查询”class 中定义了 100 个 SQL 查询。当然人们会立即忽略它,因为如果您需要的确切查询在那里而不是自己滚动,那么查找它更麻烦. class 最终增长到大约 1500 个查询,许多完全重复和许多未使用,大多数使用一次。完全没有意义)。 I can imagine exceptions where you wouldn't "lose" things with naming conventions, but unless you've got a use case like that this seems like a really bad idea.我可以想象一些例外情况,你不会通过命名约定“丢失”一些东西,但除非你有这样的用例,否则这似乎是一个非常糟糕的主意。

  • Breaking out your constants into enums gives you type-safe references.将常量分解为枚举可为您提供类型安全的引用。 It also makes things conceptually easier to deal with.它还使事情在概念上更容易处理。 Compare:相比:

- -

public class Constants { 
   String WORK_ADDRESS;
   String WORK_PHONE;
   String HOME_ADDRESS;
   String HOME_PHONE;
}

with

public enum ADRESS{ WORK, HOME }
public enum PHONE { WORK, PHONE }

Which would you rather work with?您更愿意与哪个合作?

Performance is highly unlikely to be the problem with this design.性能极不可能成为这种设计的问题。 RAM is cheap.内存很便宜。 (Cue the usual quote: Premature optimization is the root of all evil. ) (提示通常的引用:过早的优化是万恶之源。

On the other hand, I'm not quite sure how any client developer can remember and use 4000+ constants.另一方面,我不太确定任何客户端开发人员如何能够记住和使用 4000 多个常量。 Can you give us an idea what sort of object this is?你能告诉我们这是什么类型的 object 吗?

You may, depending on details you haven't given us, find it useful to collect constants into enum s.根据您没有提供给我们的详细信息,您可能会发现将常量收集到enum中很有用。 Stateless enums can be easier to understand than public static final variables if there are some natural groupings you can take advantage of.如果有一些可以利用的自然分组,无状态枚举比public static final变量更容易理解。

What happens when you allocate in static is that it surely won't be freed in the runtime of your app.当您在 static 中分配时会发生什么,它肯定不会在您的应用程序运行时被释放。

so what?所以呢?

if you don't create them static then they'll be duplicated through every instance of your classes.如果您不创建它们 static 那么它们将在您的类的每个实例中复制。

what you don't want to do is to set static huge amounts of data such as images or GUI an image takes up a lot more than a few fields;你不想做的是设置 static 大量的数据,例如图像或 GUI 一个图像占用的不仅仅是几个字段;

4000 constants of surely int (4 octet) = 16000 octets not even the size of an icon ^^ 4000 个常量肯定是 int(4 个八位字节)= 16000 个八位字节,甚至不是图标的大小 ^^

I would point out hte Javadoc to prove my point我会指出 hte Javadoc 来证明我的观点

http://download.oracle.com/javase/1.4.2/docs/api/constant-values.html#java.awt.event.KeyEvent.CHAR_UNDEFINED http://download.oracle.com/javase/1.4.2/docs/api/constant-values.html#java.awt.event.KeyEvent.CHAR_UNDEFINED

this is the KeyEvent decleration in Java, check the out declarations ^^这是 Java 中的 KeyEvent 声明,请查看声明 ^^

Unless you are creating large arrays or very long strings, 4000 data values isn't going to be a lot of memory.除非您正在创建大型 arrays 或非常长的字符串,否则 4000 个数据值不会是很多 memory。 I think that post you cited was talking about much larger amounts of data.我认为您引用的那篇文章谈论的是大量数据。

Another approach is to read the values from a preferences file.另一种方法是从首选项文件中读取值。

Perhaps the constants be modularized into a collection of classes, so the more rarely used ones will be loaded only on demand.也许常量被模块化成一个类的集合,所以很少使用的常量只会按需加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM