java/oops 中的静态变量和动态变量有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2198693/
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-08-13 04:38:26  来源:igfitidea点击:

What is the difference between a static variable and a dynamic variable in java/oops?

java

提问by vini

please someone tell me the difference between a 'static variable' and a 'normal variable' in oops or in java. Also their usage if possible.

请有人告诉我oops或java中“静态变量”和“普通变量”之间的区别。如果可能的话,还有它们的用法。

回答by Jon Skeet

A static variable is usually one associated with a type. Compare this with an instance variable, which is associated with a particular instance of a type, or a local variable, which is associated with one particular call to a method.

静态变量通常与类型相关联。将此与与类型的特定实例相关联的实例变量或与对方法的特定调用相关联的局部变量进行比较。

I don't know of any standard definition of "dynamic variable" - where have you come across this terminology?

我不知道“动态变量”的任何标准定义 - 你在哪里遇到过这个术语?

回答by GuruKulki

Static variables are those which are at the class or type level. And there will be only one copy of it is available to the all instances of that class type.

静态变量是处于类或类型级别的变量。并且只有一个副本可供该类类型的所有实例使用。

And there is no concept of dynamic variables as for as i know. If you came across about this concept at some particular context then mention that, might be helpful to explain you.

据我所知,没有动态变量的概念。如果您在某些特定上下文中遇到过这个概念,请提及它,这可能有助于解释您。

EDITED: to answer your question of difference between 'static int' and 'int'.

编辑:回答您关于“静态整数”和“整数”之间差异的问题。

Say suppose you have a class as

假设你有一个类

           public class StaticInfo{

            private static int count;
            private int variable;
            //.. say setter and getters for variable
            //.. static setter and getters for count;
          }

So if you create 2 objects of the type StaticInfo then these two will have two different 'variable' member but one common count member which is a class member.

因此,如果您创建 2 个类型为 StaticInfo 的对象,那么这两个对象将具有两个不同的“变量”成员,但一个公共计数成员是类成员。

hope it is clear now.

希望现在清楚了。

回答by fastcodejava

All variables are dynamic unless you make them final. Static is just another beast altogether.

所有变量都是动态的,除非你制作它们final。静态只是另一种野兽。

回答by Vinay Pandey

Static variable is instantiated once in life time of the Type.

静态变量在类型的生命周期内实例化一次。

For a class Age if you have a static variable static int staticAge;

对于一个类 Age 如果你有一个静态变量 static int staticAge;

and another variable as instance variable int instanceAge;

和另一个变量作为实例变量 int instanceAge;

the value assigned to staticAge will be same for all the instance of Age because same variable will shared between all the objects.

分配给 staticAge 的值对于 Age 的所有实例都是相同的,因为所有对象之间将共享相同的变量。

the value to instanceAge will be specific to the object of Age.

instanceAge 的值将特定于 Age 的对象。

回答by J?rg W Mittag

That question doesn't make much sense. Java doesn't have dynamic variables. CommonLisp has them, for example, but Java doesn't.

这个问题没有多大意义。Java 没有动态变量。例如,CommonLisp 有它们,但 Java 没有。

回答by MRT

Static variables (should) remain the same e.g. temperature of a water bath, k constant of a particular spring. Dynamic variables change as the experiment progresses e.g. air temperature and pressure, amount of natural light.

静态变量(应该)保持不变,例如水浴的温度、特定泉水的 k 常数。动态变量随着实验的进行而变化,例如空气温度和压力、自然光量。

回答by ritesh9984

In java static variable is created by the using of 'static' keyword in front of variable datatype.

在java中静态变量是通过在变量数据类型前面使用'static'关键字来创建的。

   static int count

If you are going for concept of static variable then a static variable is not created per object instead of this, it created only one copy for class . here find the example of code in java

如果您要使用静态变量的概念,那么不会为每个对象创建一个静态变量,而不是这个,它只为 class 创建了一个副本。在这里找到java中的代码示例

    class Company{

        static String companyName;
        String branch;
    }

    class Car{
            static String carName;
            String model; 
        }
        public class Server{
            public static void main(String ar[]){
                Company company1 = new Company();
                Company company2 = new Company();
                Company company3 = new Company();
                Car car1 = new Car();
                Car car2 = new Car();
                Car car3 = new Car();
            }
        }

In the above program 'Company' and 'Car' class have 3-3 objects but for static variable only one copy will be create and none static variable have 3 separate memory allocation So in 'Company' class companyName variable will create only once where branch variable will create 3 times for each object same thing applies on Car class.

在上面的程序中,'Company' 和 'Car' 类有 3-3 个对象,但对于静态变量,只会创建一个副本,没有静态变量有 3 个单独的内存分配所以在 'Company' 类中,companyName 变量将只创建一次 where 分支变量将为每个对象创建 3 次,同样的事情适用于 Car 类。

In short static variables memory is shared among all objects of class and can be modified.

简而言之,静态变量内存在类的所有对象之间共享,并且可以修改。

Dynamic variable means you want to create variable of class dynamic which is not possible instead of this you can initialize variable on dynamic using java reflection.

动态变量意味着您要创建动态类的变量,这是不可能的,而不是您可以使用 Java 反射在动态上初始化变量。

回答by ritesh9984

Consider a class having static and dynamic variables.

考虑一个具有静态和动态变量的类。

  • Dynamic variables : When instance of the class is created, each object has its own copy of dynamic variables. Values of this variables will be different for each object, whatever the value is assigned to it in that object.
  • Static variable : These are class level variables. The value of these variables will be shared among all the objects of the class. When one of the object changes its value that will be the latest value available for other objects. That means these are the shared variables.
  • 动态变量:当创建类的实例时,每个对象都有自己的动态变量副本。这个变量的值对于每个对象都是不同的,无论在那个对象中分配给它的值是什么。
  • 静态变量:这些是类级别的变量。这些变量的值将在类的所有对象之间共享。当对象之一更改其值时,该值将成为其他对象可用的最新值。这意味着这些是共享变量。