CSCI 1301

Static

1. Learning Objectives

  • What is static keyword
  • When you declare a class property static, how does it change that property
  • When you declare a class method static, how does it change that method
  • When you declare a an entire class static, how does it change that class
  • How to call static class members and how to use a static class

2. Why static

There may be scenarios where certain properties of methods should be members of the class type, rather than any specific instance. A typical example of this is having a value that should be shared between every single instance of the class.

static keyword allows us to achieve this behavior. static keyword can be applier to to any class member: property or method. We can also make an entire class static. When marking some property or method static, only 1 copy of that static member exists - regardless of how many instances of the class are created.

Next you will learn more about how static keyword affects the behavior of a property, method, or class.

3. Static Class Members

We can make any class property or method static by adding the keyword static after its access modifier, as shown in this example:

When you inspect this code you will some significant differences in how it is being used:

1. Static properties and methods can be access before instantiating an object

In other words: the static member can be used before instance of the class has been created (this is NOT true for non-static class members)

2. Static members are initialized before the static member is accessed for the first time.

which explains why they can be used before any objects are instantiated

3. There is only one copy of each static property

This is different from instance variables, where each object holds its own value of an instance variable.

4. Static members cannot access non-static fields in their containing class

More about this soon

5. Static member is always accessed by the class name, not an instance name (!)

Within the Main method, when calling static methods, instead of referring to a name of an object, we use the name of the class instead. Here SimpleExample is the name of the class this is an important difference.

1// pay attention to this syntax
2SimpleExample.GetX();

4. Mixing Static and Non-Static Members

When you have a non-static class with some static members, the following rules apply:

  • Static methods cannot access non-static class members
  • For a static method to access non-static data of an object, you have to pass that information as an argument to a static method
  • When working within the context of a non-static method, you can access static class properties and methods.

The following example demonstrates all of these principles. Pay special attention to the difference of calling a static method and calling a non-static method from the Main method.

5. Static Classes

There may be situations where you want to create a class that does not need any properties. A typical scenario is a utility class with several helpful methods that can be called in different places throughout a program. Built-in Math class in .NET framework is an example of such utility class.

You can consider marking a class static if any of the following apply:

  • it has no properties, or
  • none of the class properties should not hold different values for different objects
  • creating multiple instances should not be allowed

If the static keyword is applied to a class, all members of that class must also be static. No instance of a static class can be created -- you cannot use the new operator to create a variable of this class type. Because static class has is no instance variables, access the members by referring to the class name.

The following example demonstrates these principles


6. Summary

What is static keyword

Static keyword is use to indicate some property, method is a member of class type, rather than any specific instance. When applied to a class, static keyword changes a class so that it cannot be instantiated.

When you declare a class property static, how does it change that property

Static property is a member of a class rather than any individual instance; this means every instance can see the same value for a static property.

When you declare a class method static, how does it change that method

Static member can be called before a class instance has been created. Static method can access other static class members, but cannot access instance variables or non-static methods.

When you declare a an entire class static, how does it change that class

Static class cannot be instantiated. All of its members must also be static. You can only have 1 copy of a static class; this is different from a non-static class, where you can create unlimited number of objects.

How to call static class members and how to use a static class

static class members and class must be accessed using the class name (not instance name)

7. Review Problems

View review problem for this topic