简体   繁体   中英

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. 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'.

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.

hope it is clear now.

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

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

and another variable as instance variable 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.

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

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

   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 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.

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.

Static variables (should) remain the same eg temperature of a water bath, k constant of a particular spring. 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 . Static is just another beast altogether.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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