Static Class

Static classes in programming are classes that cannot be instantiated and contain only static members, including static methods, properties, and fields. They are typically used to group related utility functions or constants that do not require instantiation of objects. Static classes provide a convenient way to organize and access common functionality without the need to create instances of the class, making them useful for scenarios where stateless, shared functionality is needed across the application. A common use case for static classes is in defining helper functions for tasks like mathematical calculations, string manipulation, or file operations.

Select Languages

Examples

C

// No Native Support or Implementation

C#

public static class Helper {
  public static double CircleA(int radius) {
    double power = Math.pow(radius, 2);
    return (double)(3.14 * power);
  }
  public static double SquareA(int length) {
    return (double)(Math.pow(length, 2));
  }
};

double circle = Helper.CircleA(3);
Console.WriteLine(circle);
      
double square = Helper.SquareA(4);
Console.WriteLine(square);

C++

#include <iostream>
#include <math.h>
using namespace std;

// Not used in classes
// used in methods and variables only
class Helper {
  public:
    static double CircleA(int radius) {
      double power = pow(radius, 2);
      return (double)(3.14 * power);
    }

    static double SquareA(int length) {
      return (double)(pow(length, 2));
    }
};

double circle = Helper::CircleA(3);
cout << circle << endl;
      
double square = Helper::SquareA(4);
cout << square << endl;

Go

// No Native Support or Implementation

Java

import java.lang.Math;

public static class Helper {
  public static double CircleA(int radius) {
    double power = Math.pow(radius, 2);
    return (double)(3.14 * power);
  }
    
  public static double SquareA(int length) {
    return (double)(Math.pow(length, 2));
  }
}
  
double circle = Helper.CircleA(3);
System.out.println(circle );
    
double square = Helper.SquareA(4);
System.out.println(square );

JavaScript

class Helper {
  static CircleA(radius) {
    const power = Math.pow(radius, 2);
    return 3.14 * power;
  } 
  static SquareA(length) {
    return Math.pow(length, 2);
  }
}

const circle = Helper.CircleA(3);
console.log(circle);

const square = Helper.SquareA(4);
console.log(square);

Kotlin

class Helper {
  companion object {
    fun CircleA(radius:Double):Double {
      val power = radius.pow(2.0);
      return 3.14 * power;
    }
    
    fun SquareA(length:Double):Double {
      return length.pow(2.0);
    }   
  }
}

// convert type to match function
val circleR = 3;
val circle =
  Helper.CircleA(circleR.toDouble());
println(circle);

val squareR = 3;
val square =
  Helper.SquareA(squareR.toDouble());
println(square);

MatLab

classdef Helper
  methods(Static)
    function area = CircleA(radius)
      area = (radius.^2) * 3.14;
    end
    
    function area = SquareA(length)
      area = length.^2;
    end
  end
end

circle = Helper.CircleA(3);
disp(circle);

square = Helper.SquareA(4);
disp(square);

PHP

class Helper {
  public static function circleA($radius) {
    $power = pow($radius, 2);
    return (3.14 * $power);
  }
  public static function squareA($length) {
    return pow($length, 2);
  }
};

$circle = Helper::circleA(3);
echo $circle . "\n";

$square = Helper::squareA(4);
echo $square . "\n";

Python

class Helper:
  @staticmethod
  def circleA(radius):
    return (3.14 * (radius ** 2))
  
  @staticmethod
  def squareA(length):
    return length ** 2
    
circle = Helper.circleA(3)
print(circle)

square = Helper.squareA(4)
print(square)

R

// No Native Support or Implementation

Ruby

class Helper
  class << self
    def circleA(radius)
      return 3.14 * (radius ** 2)
    end
    
    def squareA(length)
      return length ** 2
    end
  end
end

puts Helper.circleA(3)
puts Helper.squareA(4)

Rust

// No Native Support or Implementation

Scala

// No Native Support or Implementation

Swift

import Foundation

struct Helper {
  static func CircleA(radius: Int) -> Double {
    return 3.14 * pow(Double(radius), 2.0);
  }
  static func SquareA(length: Int) -> Double {
    return pow(Double(length), 2.0);
  }
}

var circle = Helper.CircleA(radius: 3);
print(circle);
    
var square = Helper.SquareA(length: 4);
print(square );

TypeScript

class Helper {
  static CircleA(radius:number):number {
    const power = Math.pow(radius, 2);
    return 3.14 * power;
  } 
  static SquareA(length:number):number {
    return Math.pow(length, 2);
  }
}

const circle:number = Helper.CircleA(3);
console.log(circle);

const square:number = Helper.SquareA(4);
console.log(square);

Copyright 2025. All Rights Reserved. IronCodeMan.