Difference between Static and Instance methods (and when to use one or other)

Updated on August 30, 2018 in [A] C# .Net
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on August 30, 2018

When following the Brackeys C# course on YT, (ep 10: Classes) we learn about Classes, instantiating classes constructs and modifiers. The Private and Public modifiers are crystal clear and I understand the reason they exist. What I still do not understand is the difference between Static and Instance methods. In the episode, we can see that the goal is achieved in both ways. However, why would you choose one way over the other? The amount of code written seems to be fairly the same. Before answering, please also checkout the video https://www.youtube.com/watch?v=s2hHjpZaSyI and let me know your thoughts. 

I have read posts on Stack Overflow and MDN but I think the explanation is somewhat too technical. To give you an idea of what you are dealing with: Im a frikkin graphic designer, turned lawyer, turned developer 😀 Nice meeting ya – 

  • Liked by
Reply
0 on August 30, 2018

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.

  • Liked by
Reply
Cancel