The only difference is static methods and variables do not need a instance of the class.
What that means is you can call methods from other places without needing a reference to anything. I know it’s a weird concept to try an understand, so I’ll give some examples to hopefully help.
A static variable may be something like a counter that could keep track of all instances (but not references) of that class. Something like this:
public class Animal
public static int numberOfAnimals = 0;
public string name;
public Animal(string animalName)
name = animalName;
numberOfAnimals += 1;
Here we have a simple class with a name for the animal, and a static counter. You can only get the animal’s name via the instance (or a reference to the instance), but the counter can be accessed anywhere you can also access the Animal class. This is useful as a static variable because we don’t need each individual animal tracking how many there are. We can just use the Animal class itself and save on memory by only storing it once.
A more useful scenario is one you’ve probably already used if you’ve ever used Unity.
Vector3 is a class, not a variable like a string or int.
and Vector3.Distance() is a static method. Notice how we didn’t need to make a new Vector3 to access that function. Just like the animal example above, all we need is access to the Vector3 class, and we can use all the static variables and methods within it, no reference required.
Edit: I forgot to mention this, and this is probably one of the biggest things for static stuff.
THERE CAN ONLY BE ONE
That was kinda implied in the post, but just to clarify.
The numberOfAnimals variable. That’s the only one. You can make a million animals, but that is still the only numberOfAnimals. Not one for each. This is especially important if you have static references. That’s when you could have something like this
public class Farm
public static Animal dog;
public Animal animals;
Since the dog is static, every time you do Farm.dog you will get the exact same animal every time (until you replace it). And just like the numberOfAnimals, this will be the exact same Animal instance, no matter how many farms you make.