Statics

Someone please let me know if this is correct.

Going with Sam’s lego analogy:
Classes are considered molds and the blocks that come out of the molds are instances.
We know that all blocks (instances) have properties such as color and the number of studs which are declared as variables of that class.

It is obvious that a 6 stud mold (class) will never produce an 8 stud block. So we can say that the variable numberOfStuds of the class 6 stud mold is static.

Actually, i think you are getting confused between statics and constants.
Statics are not fixed values, they can be changed. A static variable is a value which is available to the Class itself and to all instances of that class. Instance variables are ones which are available to each specific instance.

Consider this example Window Class:

Here x and y are instance variables and are specific to each window instance ( window object) we create whereas pricePerUnitArea is a static variable and available to all window instances, it can even be changed as u can see below.

Here we have created two instances a and b of the Window.
To access an instance variable we have to access it through the instance.
ie. to get x and y we have to first ask- x and y for which window ? a or b ? so we can get x and y for each window specifically using a.x, a.y, b.x, b.y.
To access the static variables we can access them directly through the class itself.
ie. Window.pricePerUnitArea.
This single value is available for calculating price for all windows we create and is not specific for a particular window. We can even change this value and then calling the price method on each window will reflect that change.

I hope this explains it :slight_smile:

3 Likes

So it sounds like scope.

The static variables are defined at the class level and therefore available to be used by all of the instances. But if the static variable is changed, it will impact all instances of that class.

So a question would be, can an instance change that static variable of the class?

Also, if I was an instance of another class, I can call a static variable in another class by referencing the class?

What about referencing an instance variable of another class? Is that possible?

I think i might confuse you more by trying to explain this stuff, as i myself am very new to coding and jumped into c# quite recently.The questions you asked also relate to access modifiers and not just statics.

I suggest this video for understanding statics more clearly:

For access modifiers, u can watch vids 49-51.I highly recommend this playlist.

If you are finding it difficult to follow the code part in the unity videos, i suggest first spending some time learning c#. I did the same and now i find it much easier following the videos here. The unity instructors here do a great job at explaining the code but they can only do so much while also trying to explain Unity stuff.

1 Like

That video clarified it much better. Thank you.

It definitely reminds me of pointers from a memory efficiency perspective.

So in summary:
If a class has a member that will be used over and over again by its instances, you want to make it static so you aren’t duplicating those members in memory over and over every time an instance is created.

Statics not only allow you to make the memory more efficient but also allows you to access those static members without creating an instance since it is on the class itself.

1 Like

Yes exactly.
A static member might not even be used by an instance. For example we can create a static variable which keeps track of the number of instances of that class we have created. It may be of use to us but not to any instance itself.

Methods can also be static which don’t need instances to be created for their usage.
eg. Random.Range() and Input.GetKey().

1 Like

So in your example, just like the example in the video you shared below, you have:

Window a = new Window(2, 3);

I’ve been a bit confused since Window is both the name of the class and the method. Are you making a new instance of the Window object when you say “Window a” or when you say “new Window.”

I’m just confused as to why a method is being declared as “new.”

In the example above, Window is the class. Each class will have a constructor, in C# the constructor is a method of the same name as the class, so;

public Window(int x, int y)
{
    // ...
}

is the constructor for the Window class. In order to create an instance of the Window object you use the new keyword.

Window a is defining a variable named a of type Window. It is then initialised with a new instance of the Window class.

In Unity however, if your classes are inheriting from MonoBehaviour you do not do this, but you can if your classes do not inherit from MonoBehaviour.

As an example, I may have a Player class which retains some data about the Player, but because I don’t need to use any of the Unity functionality, I do not inherit from MonoBehaviour. A script attached to a GameObject in the scene might use;

private void Start()
{
    Player player = new Player();
}

This would intialise the player variable to represent a new instance of the Player class (object).

Hope this helps :slight_smile:

1 Like

Privacy & Terms