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

前端 未结 8 814
情话喂你
情话喂你 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 13:11

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

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

    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.

    0 讨论(0)
提交回复
热议问题