A static keyword is used to create a copy of the variable or the class, or any method, anything with which you have used this keyword. This replica is then shared among all the objects created at the level of the class. A static allocation is always declared with the code and once declared cannot be altered. Whereas dynamic is done at runtime by the compiler. Similarly, the static keyword that is used with any variable, class, or method, made the variable, class, or any instance not to be accessed by every other object of the class.
Implementation of Static
Example # 1: Static Variable
The static variable is created through the keyword static. When we make the variable static, a copy of this variable is created and is shared between all the objects at the class level. We do not need to access the static variable through an object, but it is accessed by using the name of the class with the variable. To elaborate on the concept of static variables, we will declare it in the class. It is suggested to use the system library, in case you are using the classes, and structures in the program. A variable is created that contains a value of the color of a vehicle. The basic syntax for static variable declaration is given here.
Now, we will declare another class for the main program to be written inside. Inside the program, we will only display the value of the color of the vehicle, declared in the first class. As the name of the class is a vehicle, this class name will be used with the variable name.
The value will be displayed, as the variable is accessed in the console statement.
We will ride the source code in the text editor and then execute it in the terminal. For the execution of code, we need a compiler in Linux to compile the C sharp code. This compiler is MCS. After the compilation, execution of the cs file is done through Mono by using the .exe extension for executing the file.
$ mono file.exe
You can see that the value of the static variable is accessed and displayed.
Example # 2: A Static Function
Similar to the static variable, static methods can also be declared by using a static keyword with their name at the time of declaration. The static methods are accessed through the name of the class. It is a property of the static method that can access the static and non-static fields. We will discuss it later. But first, let us examine the code we have quoted here, in which a static variable is declared outside the function in the class sample, making it globally accessed from another class. If it is declared inside the function, then it will become a local variable, and will not be accessed by other classes.
After the variable is assigned, the static function ‘total’ is declared to print the value of the variable. But this function will be executed through the main program. Now, we will declare the class world, and then inside it, the static main program is declared. The static method is accessed by using the name of the class. Separate objects for the class are not created here.
Save the code and then execute it, you will see that the total function is called through the main program, and hence value is displayed.
Now, we will create both static and non-static functions inside a single class. The static function is accessed through the name of the class, but to call the non-static function, we need to create a class instance. The class test is created and two functions display1() and display2() are declared here. Display1() is a non-static function, whereas the second method is a static one. The difference in both methods is because of the static keyword used with the function name.
Public static void display2()
After the declaration of functions, a class program is created. The main program will create an object of the ‘test’ class, to access the non-static method. For this purpose, dynamic creation takes place through a ‘new’ operator.
After the object has been created, now call the non-static function through the object.
Whereas, the static method is called through the class name.
The object will access the non-static method, and a class name will be used in the case of the static method. So on execution, the respective outputs will be displayed.
Example # 3: Static Class
A static class is declared just like the static function by using the name ‘static’ along with the class name.
All the variables or functions that are declared inside the static class are declared static. In other words, this class cannot have any child class further, or you can say that no one can inherit this static class from another.
So inside the class, a static data member is declared to store a value.
Another public class named Linux is created, having the main program. The static variable is accessed through the name of the class.
Example # 4: Static Constructor
A static constructor is created with the keyword static inside the class. When we create an object of the class, the normal constructor is executed, but before the execution of the constructor, the static constructor is executed. We have declared both the static and the normal instance constructor.
Public G1(int j);
A function of g1_detail is created to display the entered information of the person.
After defining the constructors, now we will create a main program inside the class. In the static main program, the class object will be created.
By making this object, both the constructors will be called. And then the function is called through this object. The parameter is for the instance constructor.
So, the results will contain the output of both the constructors and then the function. After that, another object is created.
By creating the second object, the instance constructor will be called only not the static one, because the static constructor is called once before the instance constructor, not every time.
By calling the function again through the new object, new values will be transferred to the function.
A static keyword is used to create a copy of an item that is distributed among all the objects of that class. A static word is attached with the container, you want to make static. Static can be a class, data variable, a constructor, or a method as well. C sharp can use the static keyword for all of these creations of programming. A static class can never be inherited by another class, and by making a class static, all its data variables, and constructors are declared static to make it sealed to other classes.