vb.net 对于各种编程语言和环境,静态变量通常意味着什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14892847/
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
What does static variable in general mean for various programming language and circumstances?
提问by user4951
Static variables are usually: (in most programming languages) shared, persistent, and allocated on the code section of the program
静态变量通常是:(在大多数编程语言中)在程序的代码部分共享、持久和分配
But what does that have anything to do with the word static? What is so static about that? I thought staticmeans doesn't change?
但这与静态这个词有什么关系?什么是静态的?我认为static手段不会改变?
For example, in vb.net static is written shared and that means a member function that can be accessed without object instantiation. Static within function usually means that the variable life time is the life time of the whole program. It seems that static variables are stored on the code section of the computer. Am I correct in my understanding based on the example?
例如,在 vb.net 中,静态被写入共享,这意味着成员函数无需对象实例化即可访问。函数内静态通常意味着变量生命周期是整个程序的生命周期。似乎静态变量存储在计算机的代码部分。根据示例,我的理解是否正确?
回答by JBL
Well, I think the keyword is appropriate. It means the variable you declare as static will remain stored at the same location throughout the whole execution of your program.
嗯,我认为关键字是合适的。这意味着您声明为静态的变量将在整个程序执行过程中保持存储在同一位置。
I thought static means doesn't change
我认为静态意味着不会改变
This corresponds to the constkeyword. Const implies it doesn't change, static implies it doesn't "move", as to it stays stored at the same location.
这对应于const关键字。Const 意味着它不会改变, static 意味着它不会“移动”,因为它保持存储在同一位置。
回答by James Kanze
In general, what doesn't change with something that is static in a programming language is whether it is alive or not. Static variables are always alive; they have a single instance which comes into being either at the beginning of the program or the first time they are visible, and lasts until the end of the program. Non-static variables come and go, as blocks are entered and left, or as class instances are created and destroyed.
一般来说,编程语言中静态的东西不会改变的是它是否存在。静态变量永远活着;它们有一个单独的实例,在程序开始时或第一次可见时产生,并持续到程序结束。非静态变量来来去去,随着块的进入和离开,或者随着类实例的创建和销毁。
In C++, for reasons of C compatibility, static, when applied to
variables at namespace scope, has a completely unrelated
meaning: it means that the variable has internal, rather than
external linkage, and is not visible in other translation units.
Why the word static was adopted for this in early C, I don't
know; I can only guess that they needed something, and didn't
want to introduce a new keyword. (Originally, in the very
earliest versions of C, variables at file scope obeyed the rules
of a Fortran named common block: all variables of the same name
referred to the same storage.) Looking back, of course (with 20/20
hindsight), the default for variables at file scope should have
been internal linkage, with a special keyword (public?) to say
that the variable had external linkage. But this was a lot less
obvious in the early 1970's.
在 C++ 中,出于 C 兼容性的原因,static 当应用于命名空间范围内的变量时,具有完全不相关的含义:这意味着该变量具有内部链接,而不是外部链接,并且在其他翻译单元中不可见。为什么在早期的 C 中采用了静态这个词,我不知道;我只能猜测他们需要一些东西,不想引入新的关键字。(最初,在 C 的最早期版本中,文件范围内的变量遵循名为 common 块的 Fortran 规则:所有同名变量都引用同一个存储。)当然,回顾一下(20/20 事后) ,文件范围内变量的默认值应该是内部链接,带有一个特殊的关键字 (public?) 表示该变量具有外部链接。但这在 1970 年代初期并不那么明显。
回答by Ramy Al Zuhouri
Static is referred to the variable storage. Inside a function call, every variable that you declare is pushed on the stack. Unlike other variables, a static variable isn't pushed on the stack, it's like a global variable, that survives the whole execution of the program, with the difference that is visible only inside the block is declared.
静态是指变量存储。在函数调用中,您声明的每个变量都被压入堆栈。与其他变量不同,静态变量不会被压入堆栈,它就像一个全局变量,在程序的整个执行过程中都存活下来,区别仅在声明的块内可见。
回答by user1725145
I think you just have to learn the meaning of "static" in computer science, and not relate it to spoken English. Especially as it applies to variables and functions, with slightly different outcomes in C.
我认为你只需要学习计算机科学中“静态”的含义,而不是将它与英语口语联系起来。特别是当它适用于变量和函数时,在 C 中的结果略有不同。
回答by Matt Wilko
The definition of the word from http://dictionary.reference.com/browse/static?s=t
这个词的定义来自http://dictionary.reference.com/browse/static?s=t
- pertaining to or characterized by a fixed or stationary condition.
- showing little or no change: a static concept; a static relationship.
- 与固定或静止状态有关或以固定或静止状态为特征的。
- 显示很少或没有变化:一个静态的概念;静态关系。
A static variable is one that maintains its state even after it goes out of scope as opposed to a non static variable which would be re-initialised every time it came back into scope - so can be thought of in terms of having a "stationary condition"or exhibits "no change"
静态变量是即使在超出范围后仍能保持其状态的变量,而不是非静态变量,每次返回范围时都会重新初始化 - 因此可以认为具有“静止状态” ”或“无变化”
回答by Derf Skren
If you can avoid it, just don't go into static for C++. In any modern language static just means there's only ever one instance and it's never destroyed. That's not too far a stretch from the English meaning, and leads nicely to a discussion of const/final/readonly and what that means.
如果可以避免它,请不要使用 C++ 的静态方法。在任何现代语言中,静态只是意味着只有一个实例并且它永远不会被破坏。这与英语的含义相去甚远,并且很好地引导了对 const/final/readonly 及其含义的讨论。
回答by Jinu Joseph Daniel
Static variable means ,there is only one copy of the variable,even if you create multiple instances of the class.That is, all objects of the specified class use the same memory location.Or if you want an example,say , we have two threads .On first thread you create a progressbar and on the second you need to update it.In this case you can define a static variable in your progressbar's class to store the progress and create one instance of the class in each thread.One thread for initialising and in the other you change the value of static variable.Since both use the same copy the progress will be available in the first thread. So static means something that doesnt change its location on creating a new instance..Or we can say something tha preserves its state ;) Blah blah blah
静态变量意味着,即使你创建了多个类的实例,变量也只有一个副本。也就是说,指定类的所有对象都使用相同的内存位置。或者如果你想要一个例子,比如说,我们有两个线程。在第一个线程上,您创建一个进度条,在第二个线程上,您需要更新它。在这种情况下,您可以在进度条的类中定义一个静态变量来存储进度并在每个线程中创建该类的一个实例。一个线程用于初始化并在另一个中更改静态变量的值。由于两者都使用相同的副本,因此第一个线程中将提供进度。所以静态意味着在创建新实例时不会改变其位置的东西..或者我们可以说一些东西保留它的状态;) 等等等等

