Presentation is loading. Please wait.

Presentation is loading. Please wait.

Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.

Similar presentations


Presentation on theme: "Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses."— Presentation transcript:

1 Static

2 2 Objectives Introduce static keyword –examine syntax –describe common uses

3 3 Static Static represents something which is part of a type –rather than part of an object Two uses of static –field –method

4 4 Instance field Each object gets own copy of instance fields class Item { public int Id; public int Price; public int Revenue;... } instance fields Item a = new Item(); Item b = new Item(); Item c = new Item(); instances Id Price Revenue Id Price Revenue Id Price Revenue a b c

5 5 Static field Only on copy of static field –created using keyword static –available even if no instances exist –also called static variable class Item { public int Id; public int Price; public static int Revenue;... } static field instance fields Item a = new Item(); Item b = new Item(); Item c = new Item(); instances Revenue Id Price Id Price Id Price a b c

6 6 Static field access Static field must be accessed through type name –compiler error to attempt access through instance Revenue 5 Item.Revenue = 5; access using class name

7 7 Meaning of static field Static field used to model shared resource –roughly analogous to global variable in other languages Revenue Id Price Id Price Id Price a b c shared by all items

8 8 Static field initialization Three options for initialization of static fields –default value –static variable initializer –static constructor

9 9 Static field default values Static fields set to default value –0 for numeric types –false for bool –'\x0000' for char –null for references class Item { public static int Revenue;... } defaults to 0 Revenue 0

10 10 Static variable initializer Can initialize static field in definition –called static variable initializer –initializer executed once –executed in textual order Assigned value is limited –can be literal, new statement, or return of static method call –cannot call regular method class Item { public static int Revenue = -1;... } initialize Revenue -1

11 11 Static constructor May supply static constructor to do initialization –syntax is same name as type with keyword static –only one allowed –no parameters –no access modifier –can only access other static members class Item { public static int Revenue; static Item() { Revenue = -1; }... } static constructor

12 12 Invocation of static constructor Static constructor invoked automatically –not called directly Timing of execution unspecified but some guarantees given –after static variable initializers –before any instances are created –before any static variables of that class are used –at most once

13 13 Initialization application Instance and static initialization often used in same class class Item { private static ConnectionPool pool; private Connection connection; static Item() { pool = new ConnectionPool(5); } public Item() { connection = pool.acquire(); } public void Dispose() { pool.release(connection); }... } pool shared by all Items return connection to pool get connection from pool create pool instance gets a connection

14 14 Static method Method can be static –can only access static members –cannot directly access non-static members –no this object associated with call class Item { private static int revenue; public static void ResetRevenue() { revenue = 0; }... } static method

15 15 Static method invocation Static method must be called through type name –compile time error to attempt call through instance Item.ResetRevenue(); call static method

16 16 Static method application Static methods often useful –for utility methods because global methods are not allowed –for methods for which an instance is not needed public class Math { public static double Sqrt(double d)... public static double Sin (double a)... public static double Log (double d)...... } public class Console { public static void WriteLine(string value)... public static string ReadLine ()...... }

17 17 Summary Static represents things associated with a type –rather than a particular instance of the type –must be accessed through type name Static uses –variables used to model shared resource –methods useful when instance not needed in call Several options for initialization –default values –inline –static constructor


Download ppt "Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses."

Similar presentations


Ads by Google