0% found this document useful (0 votes)
24 views

Lecture 7 Static Class Members

The document discusses static class members in object-oriented programming, including static fields, methods, properties, constructors, and classes. Static members belong to the class itself rather than individual objects. A static field creates a single instance shared among all class objects. Static methods can access only static fields and methods, while static properties provide a way to access static fields through get and set accessors. A static constructor runs once before any class objects are created. A static class contains only static members and cannot be instantiated.

Uploaded by

huthefh2019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lecture 7 Static Class Members

The document discusses static class members in object-oriented programming, including static fields, methods, properties, constructors, and classes. Static members belong to the class itself rather than individual objects. A static field creates a single instance shared among all class objects. Static methods can access only static fields and methods, while static properties provide a way to access static fields through get and set accessors. A static constructor runs once before any class objects are created. A static class contains only static members and cannot be instantiated.

Uploaded by

huthefh2019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Object-Oriented Programming

Lecture 7: Static Class Members


Lecturer:
Afaf Ghazi Bin Saadon
[email protected]

1
Outline

 Static Field

 Static Method

 Static Property

 Static Constructor

 Static Class

 Constant Field
2
Static Class Members
 You can define class members as static using
the static keyword.

 When you declare a member of a class as


static, it means there is only one copy of the
static member.

 You can call the static method or access the


static field by using the name of the class.
 No instance is required.
3
Static Field

 Defining a field as static makes it possible to


create a single instance of a field that is
shared among all objects created from a
single class.
 Non-static fields are local to each instance of an
object.

4
Example: Static Field
class Student
{
private string name;
private int age;
private static int numOfStudents; // Static Field

public Student(string name, int age)


{
this.name = name;
this.age = age;
numOfStudents++;
Console.WriteLine("Number of Students = " +
numOfStudents);
}
}
5
Example: Static Field
class Program
{
static void Main(string[] args)
{
Student s1 = new Student("Ali", 20);
Student s2 = new Student("Ahmed", 21);
Student s3 = new Student("Khaled", 22);
}
}

6
Example: Static Field
 In this example, the static field numOfStudents
is incremented by the constructor every time a
new object is created.

 All objects share the same instance of the


numOfStudents field, so the statement
numOfStudents++; increments the same data
every time a new instance is created.

 You cannot prefix numOfStudents with the


this keyword because numOfStudents does
not belong to a specific object.
7
Static Method
 A static method does not depend on an
instance of the class.

 It cannot access any instance fields or


instance methods defined in the class.
 It can use only fields and other methods that are
marked as static.

 The Console class and its ReadLine and


WriteLine methods are an example of static
methods.
8
Example: Static Method
class Student {
private string name;
private int age;
private static int numOfStudents; // Static Field
public Student(string name, int age) {
this.name = name;
this.age = age;
numOfStudents++;
}
public static int GetNumOfStudents() {
return numOfStudents;
}
public static void SetNumOfStudents(int value) {
numOfStudents = value;
}
} 9
Example: Static Method
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Student.GetNumOfStudents());
Student.SetNumOfStudents(10);
Console.WriteLine(Student.GetNumOfStudents());
Student s1 = new Student("Ali", 20);
Console.WriteLine(Student.GetNumOfStudents());
Student s2 = new Student("Ahmed", 21);
Console.WriteLine(Student.GetNumOfStudents());
}
}

10
Static Property
class Student {
public string Name { get; set; } // Property
public int Age { get; set; } // Property
private static int numOfStudents; // Static Field
public Student(string name, int age) {
Name = name;
Age = age;
numOfStudents++;
}
public static int NumOfStudents {
get {
return numOfStudents;
}
set {
numOfStudents = value;
}
}
11
}
Static Auto-Implemented Property
class Student
{
public string Name { get; set; } // Property
public int Age { get; set; } // Property
// Static Property
public static int NumOfStudents { get; set; }

public Student(string name, int age)


{
Name = name;
Age = age;
NumOfStudents++;
}
}

12
Static Property
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Student.NumOfStudents);
Student.NumOfStudents = 5;
Console.WriteLine(Student.NumOfStudents);
Student s1 = new Student("Ali", 20);
Student s2 = new Student("Ahmed", 21);
Student s3 = new Student("Khaled", 22);
Console.WriteLine(Student.NumOfStudents);
}
}

13
Static Constructor
 The static constructor is the special type that
does not take access modifiers or have
parameters.
 It is called automatically before the first
instance is created or any static members are
referenced.

 It is used to:
 initialize any static data.
 perform a particular action that needs to be
performed once only.
14
Example: Static Constructor
class Student {
public string Name { get; set; }
public int Age { get; set; }
public static int NumOfStudents { get; set; }

public Student(string name, int age) {


Name = name;
Age = age;
NumOfStudents++;
}

// Static Constructor
static Student() {
Console.WriteLine("Constructor will be called only once.");
NumOfStudents = 10;
}
}
15
Example: Static Constructor
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Student.NumOfStudents);
Student s1 = new Student("Ali", 20);
Student s2 = new Student("Ahmed", 21);
Student s3 = new Student("Khaled", 22);
Console.WriteLine(Student.NumOfStudents);
}
}

16
Static Class
 A static class can contain only static members.
 A static class cannot contain any instance data or
methods.

 The purpose of a static class is purely to act as


a holder of utility methods and fields.
 You can’t actually use new to create an instance
of an object using a static class.
 The compiler will report an error if you try.

 A static class cannot inherit from another class


except Object class. 17
Example: Static Class
static class Math {
public static int Max(int a, int b) {
if (a > b)
return a;
else
return b;
}
public static int Power(int x, int n) {
int pow = 1;
for (int i = 0; i < n; i++)
pow *= x;
return pow;
}
public static int Factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++)
fact *= i;
return fact;
}
} 18
Example: Static Class
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Max(9, 17) = " + Math.Max(9, 17));
Console.WriteLine("Power(2, 4) = " + Math.Power(2, 4));
Console.WriteLine("Factorial(5) = " + Math.Factorial(5));
}
}

19
Constant Field
 If a field is declared by using the keyword
const then it is a constant field.

 The constant field can’t be modified after its


declaration, so it must initialize at the time of
declaration only.

 The behavior of constant field will be similar


to the behavior of static field
 initialized only one time and doesn’t require the
instance of the class for accessing.
20
Example
class Circle
{
private double radius;
private const double pi = 3.14;

public Circle(double radius) {


this.radius = radius;
}

public double GetArea() {


return pi * radius * radius;
}

public double GetPerimeter() {


return 2 * pi * radius;
}
}
21

You might also like