8.2 Static members


8.2 Static members

This section covers static members in general. More information on static methods can be found in section 7.12.

The use of static in C# is almost identical to the use of the same keyword in Java. A class member declared without the static modifier is considered non-static or belonging to an instance. A static member of a class does not belong to an instance of this class, but rather to the whole class itself. A non-static member of a class is also known as an instance member.

A static field identifies only one storage location so that no matter how many instances of this class are created, there is only one copy of this static field for a particular application domain. [3] The following is an example showing the use of static fields and methods in C#.

[3] A .NET application domain is similar to a Win32 process.

 1:  class TestClass{  2:  3:    int InstField;        // instance field  4:    static int StatField; // static field  5:  6:    void DoThis() {  7:      InstField = 1; // same as this.InstField=1  8:      StatField = 1; // same as TestClass.StatField=1  9:    } 10: 11:    static void DoThat(){ 12:      // InstField = 1; // compilation error 13:      StatField = 1;    // same as TestClass.StatField=1 14:    } 15: 16:    static void Main() { 17:      TestClass t = new TestClass(); 18:      t.InstField = 1; 19:      TestClass.StatField = 1; 20: 21:      // t.StatField = 1;         // compilation error 22:      // TestClass.InstField = 1; // compilation error 23:    } 24:  } 

Line 12 will result in a compilation error because method DoThis is static. You cannot access a non-static member from a static context. Line 22 results in a compilation error because you cannot refer to a non-static member via its class name . A non-static member belongs to an instance of the class and should be referred to by an object reference variable referring to an instance of this class.

Line 21 results in a compilation error (this statement is okay in Java) because in C# you can only refer to a static member via its class name, never by an object reference variable.

Like Java

Static members of a class can only access other static members of the class. It is not legal for a static method to access a non-static (instance) field, or for a static field to refer to another non-static field. The reverse is not true though “ a non-static member can invoke or access both non-static and static members.

Unlike Java

Java allows you to refer to a class's static variable via a variable which references an instance of that class. In the case of C#, you cannot access a static member via an object reference variable. You can only access a static field using the class name.

Additional notes

  • In C#, a static member cannot be declared with the following modifiers: virtual , override , and abstract .

  • All C# constants (declared with the C# const keyword) are implicitly static.



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net