Home

C# Topics: Static Variables

     

Introduction

Imagine you create a class called Book. To access it in the Main() method, you can declare its variable, as we have done so far. A variable you have declared of a class is also called an instance of the class. In the same way, you can declare various instances of the same class as necessary:

 
public class Book
{
    public string title;
    public string author;
    public short  yearPublished;
    public int    numberOfPages;
    public char   coverType;
}

public class Exercise
{
    static int Main()
    {
	var written = new Book();
	var bought  = new Book();
	
	return 0;
    }
}

Each one of these instances gives you access to the members of the class but each instance holds the particular values of the members of its instance. Consider the results of the following program:

using System;

public class Book
{
    public string title;
    public string author;
    public short yearPublished;
    public int numberOfPages;
    public char coverType;
}

public class Exercise
{
    static int Main()
    {
        var first = new Book();

        first.title = "Psychology and Human Evolution";
        first.author = "Jeannot Lamm";
        first.yearPublished = 1996;
        first.numberOfPages = 872;
        first.coverType = 'H';

        Console.WriteLine("Book Characteristics");
        Console.Write("Title:  ");
        Console.WriteLine(first.title);
        Console.Write("Author: ");
        Console.WriteLine(first.author);
        Console.Write("Year:   ");
        Console.WriteLine(first.yearPublished);
        Console.Write("Pages:  ");
        Console.WriteLine(first.numberOfPages);
        Console.Write("Cover:  ");
        Console.WriteLine(first.coverType);

        var second = new Book();

        second.title = "C# First Step";
        second.author = "Alexandra Nyango";
        second.yearPublished = 2004;
        second.numberOfPages = 604;
        second.coverType = 'P';

        Console.WriteLine("Book Characteristics");
        Console.Write("Title:  ");
        Console.WriteLine(second.title);
        Console.Write("Author: ");
        Console.WriteLine(second.author);
        Console.Write("Year:   ");
        Console.WriteLine(second.yearPublished);
        Console.Write("Pages:  ");
        Console.WriteLine(second.numberOfPages);
        Console.Write("Cover:  ");
        Console.WriteLine(second.coverType);
        
        return 0;
    }
}

This would produce:

Book Characteristics
Title:  Psychology and Human Evolution
Author: Jeannot Lamm
Year:   1996
Pages:  872
Cover:  H

Book Characteristics
Title:  C# First Step
Author: Alexandra Nyango
Year:   2004
Pages:  604
Cover:  P

All of the member variables and methods of classes we have used so far are referred to as instance members because, in order to access them, you must have an instance of a class declared in another class in which you want to access them.

In your application, you can declare a variable and refer to it regardless of which instance of an object you are using. Such a variable is called static.

A Static Variable

To declare a member variable of a class as static, type the static keyword on its left. Here is an example:

public class Book
{
    static string title;  
}

In the same way, you can declare as many static variables as you want. As we saw in previous lessons, you can control access to a field using a modifier. If you apply the modifier, the static keyword can be written before or after it, as long as it appears before the data type. Here are examples:

public class Book
{
    public static string title;
    static public string author;
}

To access a static variable, you must "qualify" where you want to use it. Qualifying a member means you must specify its class. Here is an example:

using System;

public class Book
{
    public static string title;
    static public string author;
    public short yearPublished;
    public int pages;
    public char coverType;
}

public class Exercise
{
    static int Main()
    {
        var first = new Book();

        Book.title = "Psychology and Human Evolution";
        Book.author = "Jeannot Lamm";
        first.yearPublished = 1996;
        first.pages = 872;
        first.coverType = 'H';

        Console.WriteLine("Book Characteristics");
        Console.Write("Title:  ");
        Console.WriteLine(Book.title);
        Console.Write("Author: ");
        Console.WriteLine(Book.author);
        Console.Write("Year:   ");
        Console.WriteLine(first.yearPublished);
        Console.Write("Pages:  ");
        Console.WriteLine(first.Pages);
        Console.Write("Cover:  ");
        Console.WriteLine(first.coverType);

        var second = new Book();

        Book.title = "C# First Step";
        Book.author = "Alexandra Nyango";
        second.yearPublished = 2004;
        second.pages = 604;
        second.coverType = 'P';

        Console.WriteLine("Book Characteristics");
        Console.Write("Title:  ");
        Console.WriteLine(Book.title);
        Console.Write("Author: ");
        Console.WriteLine(Book.author);
        Console.Write("Year:   ");
        Console.WriteLine(second.yearPublished);
        Console.Write("Pages:  ");
        Console.WriteLine(second.Pages);
        Console.Write("Cover:  ");
        Console.WriteLine(second.coverType);

        return 0;
    }
}

Notice that when a variable has been declared as static, you don't need an instance of the class to access that member variable outside of the class. Based on this, if you declare all members of a class as static, you don't need to declare a variable of their class in order to access them. In the following example, the Title and Author fields of the Book class are accessed from the Exercise class without using an instance of the Book class:

using System;

public class Book
{
    public static string title;
    static public string author;
}

public class Exercise
{
    static void Main()
    {
        Book.title = "Psychology and Human Evolution";
        Book.author = "Jeannot Lamm";
           
        Console.WriteLine("Book Characteristics");
        Console.WriteLine("Title:  ");
	Console.WriteLine(Book.title);
        Console.WriteLine("Author: ");
	Console.WriteLine(Book.author);

        Book.title = "C# First Step";
        Book.author = "Alexandra Miles";

        Console.WriteLine("Book Characteristics");
        Console.WriteLine("Title:  ");
	Console.WriteLine(Book.title);
        Console.WriteLine("Author: ");
	Console.WriteLine(Book.author);

        Console.ReadKey();
    }
}

In the same way, you can declare a combination of static and non-static variables as you see fit. You just have to remember that, to access a static variable, you don't create an instance of the class. To access a non-static variable, you must declare a variable for the class.

 
 

Home Copyright © 2010-2016, FunctionX