Home

Static Fields, Methods, and Classes

  

Static Fields

Normally, to access a class in another method, you declare a variable for it. 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.

Here is an example:

<%@ Page Language="C#" %>

<html>
<head>

<script runat="server">
public class Book
{
    public string Title;
    public string Author;
    public short  YearPublished;
    public int    NumberOfPages;
    public char   CoverType;
}
</script>

<title>Exercise</title>
</head>
<body>

<%
    Book written = new Book();
    Book bought  = new Book();
%>

</body>
</html>

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:

<%@ Page Language="C#" %>

<html>
<head>

<script runat="server">
public class Book
{
    public string Title;
    public string Author;
    public short  YearPublished;
    public int    NumberOfPages;
    public char   CoverType;
}
</script>

<title>Exercise</title>
</head>
<body>

<%
    Book first = new Book();

    first.Title = "Psychology and Human Evolution";
    first.Author = "Jeannot Lamm";
    first.YearPublished = 1996;
    first.NumberOfPages = 872;
    first.CoverType = 'H';

    Response.Write("<pre>Book Characteristics<br />");
    Response.Write("Title:  " + first.Title + "<br />");
    Response.Write("Author: " + first.Author + "<br />");
    Response.Write("Year:   " + first.YearPublished + "<br />");
    Response.Write("Pages:  " + first.NumberOfPages + "<br />");
    Response.Write("Cover:  " + first.CoverType + "</pre>");
%>

<%
    Book second = new Book();

    second.Title = "C# First Step";
    second.Author = "Alexandra Nyango";
    second.YearPublished = 2004;
    second.NumberOfPages = 604;
    second.CoverType = 'P';

    Response.Write("<pre>Book Characteristics<br />");
    Response.Write("Title:  " + second.Title + "<br />");
    Response.Write("Author: " + second.Author + "<br />");
    Response.Write("Year:   " + second.YearPublished + "<br />");
    Response.Write("Pages:  " + second.NumberOfPages + "<br />");
    Response.Write("Cover:  " + second.CoverType + "</pre>");
%>

</body>
</html>

This would produce:

Book

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 class member and refer to it regardless of which instance of an object you are using. Such a member variable is called static. To declare a member variable of a class as static, type the static keyword on its left. Here is an example:

<%@ Page Language="C#" %>

<html>
<head>

<script runat="server">
public class Book
{
    public static string Title;
    public static string Author;
    public short  YearPublished;
    public int    NumberOfPages;
    public char   CoverType;
}
</script>

<title>Exercise</title>
</head>
<body>

<%
    Book first = new Book();

    Book.Title = "Psychology and Human Evolution";
    Book.Author = "Jeannot Lamm";
    first.YearPublished = 1996;
    first.NumberOfPages = 872;
    first.CoverType = 'H';

    Response.Write("<pre>Book Characteristics<br />");
    Response.Write("Title:  " + Book.Title + "<br />");
    Response.Write("Author: " + Book.Author + "<br />");
    Response.Write("Year:   " + first.YearPublished + "<br />");
    Response.Write("Pages:  " + first.NumberOfPages + "<br />");
    Response.Write("Cover:  " + first.CoverType + "</pre>");
%>

<%
    Book second = new Book();

    Book.Title = "C# First Step";
    Book.Author = "Alexandra Nyango";
    second.YearPublished = 2004;
    second.NumberOfPages = 604;
    second.CoverType = 'P';

    Response.Write("<pre>Book Characteristics<br />");
    Response.Write("Title:  " + Book.Title + "<br />");
    Response.Write("Author: " + Book.Author + "<br />");
    Response.Write("Year:   " + second.YearPublished + "<br />");
    Response.Write("Pages:  " + second.NumberOfPages + "<br />");
    Response.Write("Cover:  " + second.CoverType + "</pre>");
%>

</body>
</html>

Notice that when a member 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.

 
 
 
 

Static Methods

Like a field, a method of a class can be defined as static. To create a static method, type the static keyword to its left. Here is an example:

<script runat="server">
public class Book
{
    public string Title;
    public string Author;
    public short  YearPublished;
    public int    NumberOfPages;
    public char   CoverType;

    static public void CreateBook()
    {
	Title = "Psychology and Human Evolution";
	Author = "Jeannot Lamm";
	NumberOfPages = 472;
	Price = 24.95;
    }
}
</script>

Static Classes

A static class is a class whose members must be:

  1. Accessed without an instance of the class. In other words, the members of a static class must be accessed directly from (using the name of) the class, using the period operator
  2. Created as static. In other words, you cannot add a non-static member to a static class: all members, except for constants, must be static

To create a static class, precede the class keyword with the static keyword. Based on the above two rules, here is an example:

<%@ Page Language="C#" %>

<html>
<head>

<script runat="server">
public static class Square
{
    public static double Side;

    public static double Perimeter()
    {
        return Side * 4;
    }

    public static double Area()
    {
        return Side * Side;
    }
}
</script>

<title>Exercise</title>
</head>
<body>

<%
    Square.Side = 36.84;
            
    Response.Write("<pre>Square Characteristics<br />");
    Response.Write("Side:      " + Square.Side + "<br />");
    Response.Write("Perimeter: " + Square.Perimeter() + "<br />");
    Response.Write("Area:      " + Square.Area() + "</pre>");
%>

</body>
</html>

This would produce:

Square

A Static Method for a Delegate

In the above example, we had to declare a variable of the (Exercise) class before accessing the method. An alternative is to create the associated method as static. That way, you would not need to declare the variable first. Here is an example:

using System;

public delegate void Simple();

public class Exercise
{
    public static void Welcome()
    {
        Response.Write("Welcome to the Wonderful World of C# Programming!");
    }
}

public class Program
{
    static int Main()
    {
        Simple msg = Exercise.Welcome;

        msg();

        return 0;
    }
}

 

 
 
   
 

Home Copyright © 2009-2016, FunctionX, Inc.