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

前端 未结 8 813
情话喂你
情话喂你 2021-01-28 12:22

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

相关标签:
8条回答
  • 2021-01-28 12:56

    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.

    0 讨论(0)
  • 2021-01-28 12:59

    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?

    0 讨论(0)
  • 2021-01-28 13:00

    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.

    0 讨论(0)
  • 2021-01-28 13:00

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

    0 讨论(0)
  • 2021-01-28 13:01

    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.

    0 讨论(0)
  • 2021-01-28 13:11

    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.
    0 讨论(0)
提交回复
热议问题