Static Keyword in C# Tutorial | Learn with Examples
Last updated on 16th Aug 2022, Blog, Tutorials
Static Keyword in C#
The static keyword is used to prevent an instantiation of a data item.It is with classes, methods, variables, constructors, operators etc.It cannot be used with the destructors, indexers etc.
Here are 3 types of sharing using the static keyword are:
- Static Method
- Static Field
- Static Class
C# Static Class with Examples:
In c# static class can be generated by using a static modifier and the static class can contain only static members. The static class is the same as the non-static class, but the only difference is the static class cannot be instantiated. Suppose if you apply a static modifier to a class, you do not need to use the new keyword to create a class type variable. Another difference is the static class will include only static members, but the non-static class can contain both static and non-static members.
C# Static Class Syntax:
In c#, can generate a static class by applying a static keyword to the class like below.
- static class sample
- {
- //static data members
- //static methods
- }
If you observe the above syntax, apply a static keyword to the class type to generate a static class called “sample”.The methods and data members that are going to be implemented in the sample class must be static. In c#, can access members of a static class directly with the class name. For example, a static class called “User” with a method “Details()” that can access like User.Details().
C# Static Class Example:
Following is the example of explaining a static class to access data members and member functions without creating an instance of the class in the c# programming language.
using System:
- namespace Tutlane
- {
- static class User
- {
- // Static Variables
- public static string name;
- public static string location;
- public static int age;
- // Static Method
- public static void Details()
- {
- Console.WriteLine(“Static Method”);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- User.name = “Suresh Dasari”;
- User.location = “Hyderabad”;
- User.age = 32;
- Console.WriteLine(“Name: {0}”, User.name);
- Console.WriteLine(“Location: {0}”, User.location);
- Console.WriteLine(“Age: {0}”, User.age);
- User.Details();
- Console.WriteLine(“\nPress Enter Key to Exit..”);
- Console.ReadLine();
- }
- }
- }
If you observe the above example, accessing static class members and functions directly with the class name because , cannot instantiate the static class.
C# Static Class Features
Below the main features of static class in c# programming language.
- The static class in c# will consist only of static members.
- In c#, the static classes cannot be instantiated.
- C# static classes are sealed, so cannot be inherited.
- The static classes in c# will not contain the instance constructors.
You can include static members in non-static classes like regular classes by using the static keyword. For typical classes, the new keyword can be used to create an instance of a class that can access non-static members and methods but not static members and functions.
Advantages of Static Classes:
- 1. If you declare any member as a non-static member, it will get an error.
- 2. When trying to create an instance to the static class, it again creates a compile time error, because the static members can be accessed directly with its class name.
- 3. The static keyword is used before the class keyword in a class definition to declare the static class.
- 4. A static class member is accessed by class name followed by a member name.
Static Data Members:
As static class always contains the static data members, so static data members are declared by using static keyword and they are directly accessed by using the class name. The memory of static data members is allocated individually without any relation with an object.
Syntax:
- static class Class_name
- {
- public static name datamember;
- }
Static Methods:
As static class always contains static methods, so static methods are declared by using static keywords. These methods only access static data members, they cannot access the non-static data members.
Syntax:
- static class Class_name {
- public static name of method()
- {
- // code
- }
- }
Difference between static and non-static class
Static Class | Non-Static Class |
---|---|
Static class is explained using the static keyword. | Non-Static class is not explained by using static keywords. |
In static class, not allowed to generate objects. | In non-static class, allowed to generate objects using new keywords |
The data members of static class can be directly accessed by the class name. | The data members of non-static classes are not directly accessed by the class name. |
Static class always contains the static members. | Non-static classes may contain both the static and non-static methods. |
Static class does not contain the instance constructor. | Non-static class contains the instance constructor. |
Static class cannot inherit from another class. | Non-static classes can be inherited from another class. |
Static Constructor
A static constructor initiates the static data members of a class. This is done implicitly when they are referenced for the first time, that is when the class object is created. A static constructor does not contain any parameters.
- using System;
- namespace StaticConstructorDemo
- {
- public class SClass
- {
- public int x;
- public int y;
- public static int z;
- public SClass()
- {
- x = 5;
- y = 8;
- }
- static SClass()
- {
- z = 10;
- }
- public void display()
- {
- Console.WriteLine(“x = {0}”, x);
- Console.WriteLine(“y = {0}”, y);
- Console.WriteLine(“z = {0}”, z);
- }
- }
- class Test
- {
- static void Main(string[] args)
- {
- SClass obj = new SClass();
- obj.display();
- }
- }
- }
Rules :
- 1. Static constructors cannot contain any access modifiers.
- 2. Static constructors cannot be explained with arguments.
- 3. Static constructors cannot access the non-static data members.
Static Fields
Static fields in a non-static class can be explained using the static keyword. Static fields of a non-static class are shared across all the instances. So, modifications done by one instance would reflect in others.
Static class Members:
There are 2 types of C# static class members, static and non-static.
Non-static members: All members have this type as their default. If you do not use the “static” keyword for the declaration of a field / property or a method, then it can be called a “Non-static member”. The main feature of a non-static member is it will be bound with an object only.
Non-static Fields / Properties:The memory is allocated when the object is generated. Non-static Methods These techniques can carry out operations on attributes and fields that are not static.
Static Members:
- If you use the “static” keyword for the declaration of a field / property or a method, then it is called the “Static member”.
- A non-static member’s primary characteristic is that it won’t be bound to any objects.
- It is individually accessible with a class name.
- The static members are accessible directly, without even creating a single object.
Static Fields / Properties:The memory will be allocated single, without any relation with an object.
Static class in Java:
Java allows a class to be explained within another class. These are called the Nested Classes. Classes can be static which most developers are aware of, some classes can be made static in Java. Java encourages Static Instance Variables, Static Methods, Static Block, and Static Classes. The class in which the nested class is explained is known as the Outer Class Unlike top-level classes, Inner classes can be a Static. Non-static nested classes are also known as Inner classes. An inner class cannot be generated without an instance of the outer class. an inner class instance can access all of the members of its outer class, without using a reference to the outer class instance. For this reason, inner classes can help make programs easy and concise.
Static Block: If you need to do the computation in order to initialize the static variables, can declare a static block that gets executed exactly once, when the class is first loaded.
Static Variable: When declaring a variable as static, then a single copy of the variable is generated and divided among all objects at the class level. Static variables are essentially global variables. All the instances of the class can share the same static variable. Static variables can be generated at class-level only.
Are you looking training with Right Jobs?
Contact Us- Windows Azure Interview Questions and Answers
- Salesforce Architecture Tutorial
- Wrapper Class in Salesforce Tutorial
- salesforce lightning
Related Articles
Popular Courses
- VM Ware Training
11025 Learners
- Microsoft Dynamics Training
12022 Learners
- Siebel Training
11141 Learners
- What is Dimension Reduction? | Know the techniques
- Difference between Data Lake vs Data Warehouse: A Complete Guide For Beginners with Best Practices
- What is Dimension Reduction? | Know the techniques
- What does the Yield keyword do and How to use Yield in python ? [ OverView ]
- Agile Sprint Planning | Everything You Need to Know