C++: Static variable

This entry is part 61 of 61 in the series C++

A static variable takes its first initiated value and remains unchanged.

#include "stdafx.h"

#include <iostream>

using namespace std;

void main()
{
	for (int i = 2; i<5; ++i)
	{
		static int a = i;
		cout << a << endl;
	}

	system("pause");
}

static

C++: An experiment on a static variable

This entry is part 47 of 61 in the series C++

Here, we create an Account class and create several instances of the class.  On each call to the constructor, the static variable is incremented.

Though we have a few instances of the same object, as we have defined the variable as static, we are actually incrementing the same variable.

#include <cstdlib>
#include <iostream>

using namespace std;

class Account {
public:

    static int interestRate;

    static int rate() {

        interestRate++;
        
        return interestRate;

    }

    Account(int yr) {
        
        year = yr;
        
        cout << interestRate++ << endl;
        cout << interestRate++ << endl;
        cout << "Constructor called" << endl;
    }

private:

    int year;

};

int Account::interestRate = 10;

int main(int argc, char** argv) {

    Account A1(12);
    Account A2(12);
    Account A3(12);
    
    cout << Account::rate() << endl;
    cout << Account::rate() << endl;   
    cout << Account::rate() << endl;

    return 0;
}

static2

Java: Advantage of static members

This entry is part 34 of 54 in the series Learn Java

The advantage of static members is that they can be used by other classes without having to create an instance of the class. Fields should therefore be declared static when only a single instance of the variable is needed.

Methods should be declared static if they perform a generic function that is independent of any instance variables.

A good example of this is the Math class which contains only static methods and fields.

double pi = Math.PI;

Math is one of the classes that are included by default in every Java application. The reason for this is because it belongs to the java.lang package, which is always imported.

This package contains classes fundamental to the Java language, such as: String,Object and System.

package javaapplication26;

public class StaticMembers {

    public static void main(String[] args) {
        
        double pi = Math.PI;       
        System.out.println(pi);
        
    }
}

staticmembers

Java: Static

This entry is part 32 of 54 in the series Learn Java

The static keyword is used to create fields and methods that can be accessed without having to make an instance of the class.

Static (class) members only exist in one copy, which belongs to the class itself, whereas instance (non-static) members are created as new copies for each new object.

This means that static methods cannot use instance members since these methods are not part of an instance.

On the other hand, instance methods can use both static and instance members.

package javaapplication25;

class MyCircle {

    float r = 5;            // instance field
    static float pi = 3.14F; // static/class field

    // Instance method
    float getArea() {
        return newArea(r);
    }

    // Static/class method
    static float newArea(float a) {
        return pi * a * a;
    }
    
    public static void main(String[] args) {
        
        MyCircle c = new MyCircle();
        
        float x = c.getArea();
        
        System.out.println(x);
           
    }
}

To access the getArea method, we create an instance c for MyCircle class and access the method using a dot as in c.getArea().

static

C#: Static classes

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

A static class is quite similar to non static class except that a static class has no instance variable.

Or in another words, there are no variable needed to be declared inside a static class.

When you are designing a class and you find that variables are not needed inside a class, you can consider to assign the class as static.

It can be used as a convenient container for methods that just operate on input parameters

As there is no instance variable, you could not use new keyword to create a variable of the class.

Consider the following example:
using System;

namespace Math
{
    public static class Calculate
    {
        public static int Add(int a, int b)
        {
            return a + b;
        }
        static void Main()
        {
            int x;

            x = Calculate.Add(10, 15);
            Console.WriteLine(x);
            Console.ReadLine();
        }
    }
}

calculate

Class Calculate is static as I do not need variables for the class Calculate to function.  Take note also that the Add member function of the class is also static.

A static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created.

And so when I access the Add member, I can access it by directly using class Calculate as in:

Calculate.Add(10, 15);

This Add method is a mathematical function.  We can add a few other related mathematical functions such as multiply, sum etc and make them all to be static members.

You just have to ensure that they can operate on only input parameters similar to the Add method.