A metaphorical or really clear explanation about the usage of static variables or methods in java

二次信任 提交于 2019-12-11 02:59:28

问题


I've asked my teacher this a thousand times and he explained it to me just as many times, but I still don't get when to use static in variables/methods

Could someone give a metaphorical explanation for this, maybe also showing some examples where static is needed and where not?


回答1:


Do I want to access the method without an instance of the class?

If you answered yes, you probably want a static method.

private static variables can be used to share data across instances of that class, i.e. if you have:

public class Car
{
    private static int wheelsNum;
    private String company;
    private String color;
    ...
    ...
}

Then if you change wheelNum to be 2, then all cars will have 2 wheels.

For example, consider this piece of code:

Car car1 = new Car();
Car car2 = new Car();

car1.setColor("Yellow");
car2.setColor("Blue");

car1.setWheelsNum(4);
car2.setWheelsNum(2);

Then both cars will have 2 wheels, although I "didn't" mean to change the wheels number of the first car. But, as you can tell, the cars have different colors.

public static variables used without making instance of the class, wheras private static variables are not.

When you need to use a variable in a static function, you can only use static variables, so making it private to not access them from other classes.

Static methods can't access non-static methods (and the same for variables).




回答2:


I'm gonna try to explain as short and simple as possible, I am a student too. A static variable or method, DOES NOT CHANGE with every instance. Example, we have this test class:

        Class Test(){
         String name;
         static int money;
         public test(String name, int money){
           this.name = name;
           this.money = money;
         }
    //Changes money value
         public void setMoney(int money){
          this.money = money;
    }
   public int getMoney(){
    return this.money 
}
        }

Now we are gonna create 2 test instances of test(): Test test1 = new test("test1", 10); Here, the name of the instance of test1 is "test1" and the money value is 10. Test test2 = new test("test2", 20); Now, the name of the instance of test1 is still "test1", but the money value is now 20! This can be useful when you need for example counting how many instances have created, or in general counting. It is really powerful, with a static variable you can just simply change the attributes of avery instance. If I now do test1.setMoney(1000), test1.getMoney vill return 1000 and test2.getMoney will return 1000 too I hope I could help...




回答3:


You asked for examples, here's a simple one:

The Integer class has an intValue() method. It only makes sense for an instance of this class, so this method cannot be static.

This class also has a valueOf() method, that returns an instance of the class from the specified int. You don't need to have an existing instance to call this method, so this method is static.




回答4:


I finally understand, thanks to Maroun Maroun!

For other people reading this question , I figured out a clear way to explain this matter (correct me if I'm wrong though!)

Let's say we have a class named Person.

every instance of this class creates a new person with the variables that define him, so the variable "age" and "name" are assigned to this person, therefore non-static,

every instance of this class also adds 1 to the amount of persons in total, therefore this variable value is shared amongst all the persons, this variable must be static.

public class Person {


    private static int amountOfPersons;
    private String name;
    private int age;

    public static void main(String[] args) {
        Person person1 = new Person();
        Person person2 = new Person();
        person1.age = 10;
        person2.age = 20;
        person1.name="Johnny";
        person2.name="Brian";
        System.out.println(person1.amountOfPersons+" "+person1.name+" "+person1.age);
        System.out.println(person2.amountOfPersons+" "+person2.name+" "+person2.age);
    }
    public Person(){
        Person.amountOfPersons++;
    }
} 


来源:https://stackoverflow.com/questions/15827869/a-metaphorical-or-really-clear-explanation-about-the-usage-of-static-variables-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!