#695 – Static Methods Can Access Static Members

A static method can only access static members in the class that it’s defined in.

(If you pass an instance of an object to a static method, it can invoke instance members on that object).

    public class Dog
    {
        // Instance properties
        public string Name { get; set; }
        public int Age { get; set; }

        // Instance constructor
        public Dog(string name, int age)
        {
            Name = name;
            Age = age;
        }

        // Instance method
        public void Bark()
        {
            Console.WriteLine("{0} says WOOF", Name);
        }

        // Static property
        public static string DogMotto { get; protected set; }

        // Static constructor
        static Dog()
        {
            DogMotto = "Serve humans";
        }

        // Static method
        public static void AboutDogs()
        {
            // Accessing static data
            Console.WriteLine("Dogs believe: {0}", DogMotto);

            // ERROR: Can't access instance property
            Console.WriteLine(Name);

            // ERROR: Can't call instance method
            Bark();
        }
    }

Trying to access instance methods or data from a static member will generate a compile-time error.

Advertisements

#670 – Static vs. Instance Initialization

Static fields and properties are data members associated with a class itself, rather with instances of that class.  A static constructor can be used to initialize static members.  Static fields can also be initialized when they are declared.

The first time that you reference a static member, the following occurs:

  • All static fields are initialized
  • The static constructor is called
  • Your code, which accesses the static member, executes

If you reference an instance method, or create an instance of the class, before referencing any static members, the following occurs:

  • All static fields are initialized
  • The static constructor is called
  • All instance fields are initialized
  • The instance constructor is called
  • Your code, which accesses an instance member, executes

For example, initializing an instance of a Dog leads to the output shown below.

Dog d = new Dog();

#609 – Omit the Class Name for Static Members in the Same Class

Normally when you reference static members, you use the class name where they are defined to prefix the name of the member.  For example, if we have a static property in the Dog class called Motto and a static method called ListAllDogs, we’d use these static members from another class as follows:

    public class Program
    {
        static void Main()
        {
            // Reference static members in Dog from outside of Dog class
            Console.WriteLine("Dog motto: {0}", Dog.Motto);
            Dog.ListAllDogs();
        }
    }

However, if we access these static members from code within the Dog class itself, we can omit the class name when referencing the static members.

        // Dog.Bark
        public void Bark()
        {
            Console.WriteLine("{0} says Woof", Name);

            // Reference static members from within Dog class
            Console.WriteLine("Dog motto: {0}", Motto);
            ListAllDogs();
        }

#45 – Static Members of a Class

When you create a new object and use a reference variable to call methods of a class, you’re using instance members of that class.  Instance members interact with just that instance of the class.

You can also work with class members without creating an instance of the class.  Static class members operate on the class itself, rather than on instances of the class.

To invoke static members, you use the name of the class, rather than a variable that points to an instance of the class.

In the example below, we create two Person objects and then work with both instance and static methods.

 // Create 2 new Person objects
 Person p1 = new Person("Sean", 46);
 Person p2 = new Person("Fred", 28);
 string info = p1.DoReport();       // Acts on p1
 int age = p2.Age;                  // p2's Age

 int numFolks = Person.PersonCount;   // Static property
 Person.DoGeneralPersonStuff();       // Static method