简体   繁体   English

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

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

please someone tell me the difference between a 'static variable' and a 'normal variable' in oops or in java. 请有人告诉我oops或Java中的“静态变量”和“普通变量”之间的区别。 Also their usage if possible. 如果可能,还要使用它们。

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? 我不知道“动态变量”的任何标准定义-您在哪里遇到过该术语?

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. 这意味着这些是共享变量。

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'. 编辑 :回答“ static int”和“ 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. 希望现在清楚了。

Static variable is instantiated once in life time of the Type. 在Type的生命周期中实例化一次静态变量。

For a class Age if you have a static variable static int staticAge; 对于年龄类,如果您有一个静态变量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. 对于Age的所有实例,分配给staticAge的值都将相同,因为相同的变量将在所有对象之间共享。

the value to instanceAge will be specific to the object of Age. instanceAge的值将特定于Age对象。

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 在这里找到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. 在上面的程序中,“公司”和“汽车”类具有3-3个对象,但是对于静态变量,将仅创建一个副本,并且没有静态变量具有3个单独的内存分配,因此在“公司”类中,companyName变量将仅在分支处创建一次变量将为适用于Car类的相同对象的每个对象创建3次。

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反射在动态变量上初始化变量。

Static variables (should) remain the same eg temperature of a water bath, k constant of a particular spring. 静态变量(应该)保持相同,例如水浴的温度,特定弹簧的k常数。 Dynamic variables change as the experiment progresses eg air temperature and pressure, amount of natural light. 动态变量会随着实验的进行而变化,例如空气温度和压力,自然光量。

All variables are dynamic unless you make them final . 除非您将它们定为final否则所有变量都是动态的。 Static is just another beast altogether. 完全是另一只野兽。

That question doesn't make much sense. 这个问题没有多大意义。 Java doesn't have dynamic variables. Java没有动态变量。 CommonLisp has them, for example, but Java doesn't. 例如,CommonLisp有它们,而Java没有。

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

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