The `static` Keyword
An important keyword which you might have come across many times already is the static
keyword. In the C# Basics Course, it was mentioned that the static
is used when defining global variables - variables which are outside any specific method:
index.cs
1234567891011using System; class ConsoleApp { static int val = 10; static void Main() { Console.WriteLine($"The value is {val}"); } }
It was a convenient way to describe the static
keyword at that time because it was expected that the reader might not have any knowledge of objects or classes at that point, however now if you look at the code, you might realize that val
is basically a field of the ConsoleApp
class. However, note that the ConsoleApp
class is slightly different from the classes you normally create. It is because ConsoleApp
represents the program itself, and there is no object of the ConsoleApp
. You will get back to this part in a bit.
Since a class is only a blueprint, more like a hollow shell, it does not normally contain data by itself, instead you create objects using that blueprint and then store and access data from those objects - as explained before:
index.cs
12345678910111213141516171819202122232425262728using System; class Complex { public int real; public int img; public Complex(int real, int img) { this.real = real; this.img = img; } } class ConsoleApp { static int val = 10; static void Main() { Complex c = new Complex(5, 10); // We can use and update the field of 'c' which is an instance. Console.WriteLine(c.real); c.real = 7; Console.WriteLine(c.real); } }
Even inside a class, when you are accessing those fields, you are accessing them through an object. For example in a constructor, you are basically using the data that was passed by the object which called the constructor method:
index.cs
12345678public Complex(int real, int img) { // 'int real' and 'int img' contain the data which was passed // 'this.real' refers to the 'real' field of the object which is calling this constructor // same is the case for 'this.img' this.real = real; this.img = img; }
Similarly, in other methods as well, when you are accessing fields, you are basically accessing the fields of objects which call those methods, and not of the class itself because class normally does not contain any data.
However, there is a way to store data into a class directly and make a property accessible without needing to create an object. You can do that by simply making that field or method static
:
index.cs
123456789101112131415161718192021222324252627282930313233343536373839using System; class Complex { public int real; public int img; // A static field can contain data // It is set to private because we don't want it to be manually modifiable from outside // This will track the total number of 'Complex' objects created private static int numbers = 0; public Complex(int real, int img) { this.real = real; this.img = img; numbers += 1; } // A static method // A static method or field can be accessed using the 'ClassName.PropertyName' syntax (see below) public static int getTotalComplexNumbers() { return numbers; } } class ConsoleApp { static void Main() { Console.WriteLine(Complex.getTotalComplexNumbers()); // 0 new Complex(1, 2); Console.WriteLine(Complex.getTotalComplexNumbers()); // 1 new Complex(2, 3); Console.WriteLine(Complex.getTotalComplexNumbers()); // 2 } }
Since the ConsoleApp
or the main class of any program which represents the program itself cannot have any objects, you need to make their methods and fields static. This is why the Main
method is static
as well:
index.cs
1234static void Main() { // code }
1. Can a class directly store data?
2. What is the correct syntax for modifying the value
field?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 2.04
The `static` Keyword
Swipe to show menu
An important keyword which you might have come across many times already is the static
keyword. In the C# Basics Course, it was mentioned that the static
is used when defining global variables - variables which are outside any specific method:
index.cs
1234567891011using System; class ConsoleApp { static int val = 10; static void Main() { Console.WriteLine($"The value is {val}"); } }
It was a convenient way to describe the static
keyword at that time because it was expected that the reader might not have any knowledge of objects or classes at that point, however now if you look at the code, you might realize that val
is basically a field of the ConsoleApp
class. However, note that the ConsoleApp
class is slightly different from the classes you normally create. It is because ConsoleApp
represents the program itself, and there is no object of the ConsoleApp
. You will get back to this part in a bit.
Since a class is only a blueprint, more like a hollow shell, it does not normally contain data by itself, instead you create objects using that blueprint and then store and access data from those objects - as explained before:
index.cs
12345678910111213141516171819202122232425262728using System; class Complex { public int real; public int img; public Complex(int real, int img) { this.real = real; this.img = img; } } class ConsoleApp { static int val = 10; static void Main() { Complex c = new Complex(5, 10); // We can use and update the field of 'c' which is an instance. Console.WriteLine(c.real); c.real = 7; Console.WriteLine(c.real); } }
Even inside a class, when you are accessing those fields, you are accessing them through an object. For example in a constructor, you are basically using the data that was passed by the object which called the constructor method:
index.cs
12345678public Complex(int real, int img) { // 'int real' and 'int img' contain the data which was passed // 'this.real' refers to the 'real' field of the object which is calling this constructor // same is the case for 'this.img' this.real = real; this.img = img; }
Similarly, in other methods as well, when you are accessing fields, you are basically accessing the fields of objects which call those methods, and not of the class itself because class normally does not contain any data.
However, there is a way to store data into a class directly and make a property accessible without needing to create an object. You can do that by simply making that field or method static
:
index.cs
123456789101112131415161718192021222324252627282930313233343536373839using System; class Complex { public int real; public int img; // A static field can contain data // It is set to private because we don't want it to be manually modifiable from outside // This will track the total number of 'Complex' objects created private static int numbers = 0; public Complex(int real, int img) { this.real = real; this.img = img; numbers += 1; } // A static method // A static method or field can be accessed using the 'ClassName.PropertyName' syntax (see below) public static int getTotalComplexNumbers() { return numbers; } } class ConsoleApp { static void Main() { Console.WriteLine(Complex.getTotalComplexNumbers()); // 0 new Complex(1, 2); Console.WriteLine(Complex.getTotalComplexNumbers()); // 1 new Complex(2, 3); Console.WriteLine(Complex.getTotalComplexNumbers()); // 2 } }
Since the ConsoleApp
or the main class of any program which represents the program itself cannot have any objects, you need to make their methods and fields static. This is why the Main
method is static
as well:
index.cs
1234static void Main() { // code }
1. Can a class directly store data?
2. What is the correct syntax for modifying the value
field?
Thanks for your feedback!