Static
2
Objectives
•
Introduce static keyword
–
examine syntax
–
describe common uses
3
Static
•
Static represents something which is part of a type
–
rather than part of an object
•
Two uses of static
–
field
–
method
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
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
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
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
Static field initialization
•
Three options for initialization of static fields
–
default value
–
static variable initializer
–
static constructor
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
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
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
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
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
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
Không có nhận xét nào:
Đăng nhận xét