Earlier in this section, we discussed static methods. A quick refresher: a static method is called on the class itself, not on an instance of the class. For example, we would use a static method on the Item
class to return the Item
s on our to do list. We wouldn't use an instance method because an individual Item
only knows about itself. However, the class knows about all of the Item
s.
However, how can we make sure that the Item
class knows about all the Item
s on the list? Where are they being stored?
We need to create a static variable to hold this information.
Let's declare a static variable that maintains a list of all Item
objects like this:
using System.Collections.Generic;
...
public class Item
{
public string Description { get; set; }
private static List<Item> _instances = new List<Item> {};
...
}
...
We've added the proper using directive to use List
s. We've also added a new variable to the class called _instances
.
It's a List
that will eventually contain all Item
objects that a user creates for a to do list.
We follow best practices of encapsulation and make this variable private
. (Remember that we use camelCase with a leading underscore since it is a private field variable.)
This variable is static
because the information it holds is related to the entire class, not just a single Item
.
In order for this list to hold all Item
s, every Item
object that's created must be added to it. But where is the best place to put this code?
We know our constructor will always run when a new object is instantiated so we'll add our new code there.
...
public class Item
{
...
private static List<Item> _instances = new List<Item> {};
public Item(string description)
{
Description = description;
_instances.Add(this); // New code.
}
...
}
...
We use the keyword this
to reference the object being actively created. This is similar to how we use this
in JavaScript. Each time we call our constructor, it will create a new Item
and add it to _instances
.
Then, we explicitly define a getter method to retrieve the _instances
list:
...
public class Item
{
...
public static List<Item> GetAll()
{
return _instances;
}
...
}
...
This method returns the private _instances
field variable. However, because it returns a static variable, it must be declared static
. Both variables and methods dealing with entire classes must be static
.
Additionally, because the method we've just written is static
it must be called on the class itself, not a particular instance. Calling a method directly on a class looks like this:
Item.GetAll()
Notice how we are not calling it on a specific instance like newItem.GetAll()
, but are instead targeting the entire class by using its name directly.
In the next lesson, we'll test our static methods and variables in our to do list application following the "Red, Green, Refactor" workflow.
Static method: A method called on a class.
Static variable: A variable scoped to the class.
A static variable includes the static
keyword. For instance:
private static List<Item> _instances = new List<Item> {};
Static variable names always begin with an _
.
Lesson 16 of 20
Last updated April 14, 2022