C#: Static variables

This entry is part 23 of 71 in the series C#

The word static actually means something that stay constant, fixed or unchanged.

But when it comes to C#, a static variable does not work like a constant variable.

In C#,  A static variable comes into existence before execution of the static constructor. The initial value of a static variable is the default value of the variable’s type.

To understand how static variable works, we can consider the following 2 programs.

With static declaration
using System;

namespace StaticVariable
{
    public class Variable
    {
        public static int i;
        public void Increment()
        {
            i = i+1;
            Console.WriteLine(i);
        }
    }


    public class Program
    {
        static void Main()
        {
            Variable number = new Variable();
            number.Increment();
            Variable number1 = new Variable();
            number1.Increment();
            Variable number2 = new Variable();
            number2.Increment();
            Console.ReadLine();
        }
    }
}

static1

When we run the above code, the output in the console will be 1, 2, 3.

Now consider removing the static declaration in line 7.

Without static declaration
using System;

namespace StaticVariable
{
    public class Variable
    {
        public int i;
        public void Increment()
        {
            i = i+1;
            Console.WriteLine(i);
        }
    }


    public class Program
    {
        static void Main()
        {
            Variable number = new Variable();
            number.Increment();
            Variable number1 = new Variable();
            number1.Increment();
            Variable number2 = new Variable();
            number2.Increment();
            Console.ReadLine();
        }
    }
}

static2

The output from running the above code will be 1,1,1.

So for the first example, each subsequent run of Increment method will increase the integer i.  Whereas in the second example, after removing the static declaration, integer i remains unchanged.

So a static variable value is shared among all instances of that class. And without a static declaration, it will stay constant.

C#: Static members

This entry is part 22 of 71 in the series C#

We have learned that to access the method of an object, we create an instance and use a dot after the instance to access the method.

For example, to access the count method of the class Book:

Book book = new Book();
book.count();

This implies that we must create an object first before accessing its members.

However we have some examples that used Console.WriteLine to print values to the console. You may have noticed that you could call the WriteLine method without first having to construct a Console object.

How come we do not have to do something like this?

Console console = new Console();
console.WriteLine("blah blah");

C# (and many OOP languages) supports the notion of static members, which are class members that can be called without first instantiating the class.

The WriteLine method in System.Console is static, which explains why you can use it without first instantiating System.Console.

using System;

namespace Math
{   
    class Calculate
    {
        public static int Add(int a, int b)
        {
            return a + b;
        }
        static void Main()
        {
            Console.WriteLine(Calculate.Add(10, 15));
            Console.ReadLine();
        }
    }
}

In the above program, have declared Add method as a static member of class Calcuate.  That is why we are able to access the Add method using

Calculate.Add(10, 15)

This is done without using an instance.

Main() is static

In fact, there is another static method in the above program.

The method Main, which acts as the entry point to a class, is static because it must be called before any object is created.

calculate

 

PHP: Static variable

This entry is part 8 of 54 in the series PHP Tutorial

A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.

Consider the following example:

<?php
function final_no()
{
    static $num = 0;
    echo $num;
    $num++;
    echo "<br>";
}

final_no();
final_no();
final_no();

?>
Try The Code

The output from the above code is 0, 1, 2.

The $num was declared once as a static variable and will be initialized on the first call to final_no().  Everytime final_no() is called, $num will not be re-initialized to 0.