A Question about static, please help me.

Hello everyone, when i am learning Scripting “static”, there is a question wrap me around.

  1. What is the different between:
  • public static float a;
  • static public float a;
  1. if a public function declared to: public static void test(){}. Is there any different from non-static?

Thank you so much!! And sorry for my bother.

@Nickymouse You can find the docs here. You might also want to check out this site.

Regarding your first question, normally you write “public static float a;” but I do not think there is any difference between them (if the second one works at all).

Regarding your second question: Yes, there would be a difference. The use of static means that the method or variable does not belong to an instance of the class but to the class itself. Thus a static function/method would not be able to change variables that belong to a specific instance (I believe this is the case). So a static function would not be able to change or use a non-static variable.

Further a static function doesn’t require a reference to an instance. This means that if you have a class called “myClass” you can just call the function by using “myClass.myStaticFunction()” without having to reference a specific instance.

You should look at anything you mark with “static” that it is shared between instances. They all share this function/method or variable and it doesn’t belong to any single instance. This means that when one instance change the static variable, then the variable is changed for all instances of this class.