Learn Static variables in C# with example and how it is different than non-static variables of a class.

Let’s see a simple example that show how static variable and non-static variable also known as instance variable are declared in a class.

In below Student class numberOfStudentEnrolled is static
String name is non-static and known as instance variable

class Student {
 
	//static variable
	private static int numberOfStudentEnrolled= 0;
	
	//non- static ( instance variable) 
	private String name;	
	
	//Constructor
	public Student(String name){
		this.name = name;
	}
	
}

Static variable has only one copy in the memory and all objects of the class share the same copy.

Non- static or instance variable has their own separate copy in the memory and is not shared between different objects.

For example,

If you create objects of the class in C# as below

Student peter = new Student(“Peter”);
Student john = new Student(“John”);
Student linda = new Student(“Linda”);

All students Peter, John and Linda objects are in different memory on the heap and have their own copy of “String name” variable. So, in every memory, name is unique and not sharable to each other.

Whereas the static variable “static int numberOfStudentEnrolled” is seen by all the objects peter, john and linda. Means, all objects will see the same value of numberOfStudentEnrolled.

Static variable is seen by all objects whereas no object can see value of others instance variable

NOTE: Static variables can be accessed by both non static method and static method in C# program . But instance variable can be accessed by only non static method.

Example of Static variable in C#

In below C# example, Static variable numberOfStudentEnrolled is incremented in constructor by 1. When ever you create an object of the class student, then it will be increased by 1 automatically.

name and roll number is supplied at the time of creating an object.

You can notice that all name and roll number are unique as all objects resides in different memory.

if you fetch value of numberOfStudentEnrolled using any object, then you will see the updated value of this static variable as all objects are sharing this variable.

class Student
    {

        // static variable
        private static int numberOfStudentEnrolled = 0;

        // non- static ( instance variable)
        private String name;
        private int rollNumber;

        // Constructor
        public Student(String name, int rollNumber)
        {
            this.name = name;
            this.rollNumber = rollNumber;
            //this.rollNumber = rollNumber;

            // increment the value of Student Enrolled
            ++numberOfStudentEnrolled;
        }

       public int numberOfStudent_Enrolled()
        {

            return numberOfStudentEnrolled;
        }

      public  String getName()
        {
            return name;
        }

     public   int roll_Number()
        {
            return rollNumber;
        }
    }

    // Test program
    public class Sample
    {

        public static void Main(String[] args) {
 
		// Register 3 students
		Student peter = new Student("Peter", 123);
		Student john = new Student("John", 234);
		Student linda = new Student("Linda", 345);
 
		Console.WriteLine("Name :" + peter.getName());
        Console.WriteLine("Roll Number :" + peter.roll_Number());
 
		Console.WriteLine("Name :" + john.getName());
        Console.WriteLine("Roll Number :" + john.roll_Number());
 
		Console.WriteLine("Name :" + linda.getName());
        Console.WriteLine("Roll Number :" + linda.roll_Number());
 
		// chose any object and check total number of enrollment

        Console.WriteLine("Total Enrolled Student :"
                + john.numberOfStudent_Enrolled());
	}

    }

Output:
Name :Peter
Roll Number :123
Name :John
Roll Number :234
Name :Linda
Roll Number :345
Total Enrolled Student :3

Related Posts