Static Classes

Hi, all! I would like to know what the purpose of creating a static class is, and when I declare a class as static, what exactly happens?

I know that when I have a static variable, it becomes constant throughout all scripts utilizing it. This wouldn’t make sense for a class though.

Also, what is the purpose of static functions and what are the implications of doing so?

My best understanding so far is that using static on functions and classes, I can have a script titled ‘functions’ that are generic functions that I can call from any script. And I can have a folder called ‘Monobehaviours’ or something that contain just classes that I might need to use a lot.

#If you’re too lazy to read:

  • What happens to a class when you declare it as static?
  • What is the general use of static classes?
  • When is it important to use them?
  • Warnings / tips?

Same goes for static functions.

Thanks! - YA

Generally the point of object-oriented programming is to remove global data from the source code. When you work with the pure procedural paradigm of programming (only functions and data with no objects), you have the problem that data is generally not very well encapsulated. It is public to most of the program, and functions are free to manipulate them as they please, which can of course become dangerous and can be prone to bugs.

In the object-oriented paradigm there generally is no such thing as “global data”. Everything is stuck inside classes in one way or another. The closest thing you get to global data is things such as static variables.

First of all, a static variable which is part of a non-static class is still associated with that class. The only difference is that instead of each instance of the class having a unique copy of it, all instances share the same memory for that variable, thus it is the same variable they share as you already stated.

When you declare a static class, the only thing you do is actually telling the compiler that no members (data and functions members) can be declared as anything other than static. I think that’s what might confuse you. A static class is really the same as any other class, the only difference the static keyword does is tell the compiler that only static variables and methods can go in it, and throws you an error if you try to do it. Also, you are not allowed to instantiate a static class!

When it comes to the design issues on static classes there is a whole lot of different opinions as to when or even if you should use them (same goes for the singleton pattern, which is generally frowned upon).

The thing is that static classes goes against the object-oriented paradigm in that you declare data as global. Any part of your program can access it. And this is the reason as why you have to be careful about using them, since it is easy to introduce bugs. For example, if you have a static variable in a class which defines something like how long the program have been running (just an example here), it is easy for other client code to go in a manipulate it in such a way that it becomes incorrect during the programs lifetime, furthermore it’s hard to debug from where the change is actually coming from. So that’s one thing to be careful of.

Another thing is obviously when you start working with multi-threading and have to make sure threads don’t introduce race-conditions and such to the static data…

I would say that right now, use static classes if you feel the need for it. Just be aware of the design implications it can have on your program. Depending on your situation it might be a good idea to use it, like if you have things in your game which is only declared once and should be used by a lot of other code to do calculations. In a game I’m working on, I have static data such as a “cube dimension” which defines the width, height and depth of cubes which the game is made out of, and it’s used in many scripts to make calculations that’s depending on that data.

Other good ways to use a static class would be to library functions that simply take some input and gives you some output. You can look at classes such as the “mathf” library in Unity, which is a static class that provides you with a lot of helper functions for mathematical calculations. This kind of class does not save any data for you, it simply provides you with ways to calculate input.

Wall of text here :smiley: But hope you can use some of the information :slight_smile:

It’s been a long time since I last used statics, but I’ll try to answer as best as I remember.

A static class is essentially a library of data or functions you want performed, but you don’t want an actual object of that type to exist. Example, you may have a Gameplay class that gives you some calculations and such, but you wouldn’t want a Gameplay object.

To follow up on Nemox’s response, all static functions is a way to write “normal” functions in a fake namespace:

class utility {
  static closeEnough(float a, float b) { return (a>=b-SMALL && a<=b+SMALL); }
  const float SMALL = 0.1f;

  static sqrd(float a) { return a*a; }
    ...

You can then, without having to attach the script on a scene object, use utility.closeEnough(cats, dogs) and utility.sqrd(D).