Static members
Demonstrates static properties and methods; Go substitutes package-level identifiers, Rust associated functions.
C C23
#include <stdio.h>
// C has no static members; a static file-scope variable + name-prefixed functions express them
static int counter_count = 0; // static property: only one copy for the whole program
void counter_increment(void) { // static method
counter_count++;
}
int main(void) {
counter_increment();
counter_increment();
printf("%d\n", counter_count);
printf("Counter"); // class-name constant: expressed as a string literal/macro in C
return 0;
}
2 Counter
C++ C++26
#include <iostream>
class Counter {
public:
static int count; // static member declaration
static void increment() { count++; }
};
int Counter::count = 0; // static members are defined outside the class (inline initialization allowed since C++17)
int main() {
Counter::increment();
Counter::increment();
std::cout << Counter::count << "\n";
return 0;
}
2
C# v11.0.100
Counter.Increment();
Counter.Increment();
Console.WriteLine(Counter.Count);
Console.WriteLine(nameof(Counter)); // class name
class Counter {
public static int Count { get; set; } // static property
public static void Increment() => Count++; // static method
}
2 Counter
Go v1.27.4
package main
import "fmt"
// Go has no static members: package-level variables + package-level functions achieve the same
var count = 0
func increment() {
count++
}
func main() {
increment()
increment()
fmt.Println(count)
}
2
Java v27.0.1
public class Main {
static class Counter {
static int count = 0; // static field: one copy shared by the whole class
static void increment() {
count++;
}
}
public static void main(String[] args) {
Counter.increment(); // called via the class name
Counter.increment();
System.out.println(Counter.count);
}
}
2
JavaScript v26.13.1
class Counter {
static count = 0; // static field (natively supported since ES2022)
static increment() {
Counter.count++;
}
}
Counter.increment();
Counter.increment();
console.log(Counter.count);
2
PHP v8.6.0
class Counter {
public static int $count = 0; // static property
public static function increment(): void {
self::$count++; // access via self:: inside a static method
}
}
Counter::increment();
Counter::increment();
echo Counter::$count, "\n";
echo Counter::class; // class-name constant
2 Counter
Python v3.15.2
class Counter:
count = 0 # class attribute: shared by all instances
@classmethod
def increment(cls): # first parameter cls, operating on the class itself
cls.count += 1
@staticmethod
def describe(): # utility method receiving no self/cls
return "Counter"
Counter.increment()
Counter.increment()
print(Counter.count)
print(Counter.describe())
2 Counter
Rust v1.99.1
struct Counter {
count: i32,
}
impl Counter {
// associated function: the first parameter is not self; called via Counter::new
fn new() -> Counter {
Counter { count: 0 }
}
fn increment(c: &mut Counter) {
c.count += 1;
}
}
fn main() {
let mut c = Counter::new();
Counter::increment(&mut c);
Counter::increment(&mut c);
println!("{}", c.count);
// true global mutable state needs static + Mutex/Atomic and is generally discouraged
}
2
TypeScript v26.13.1
class Counter {
static count = 0; // static field
static increment(): void {
Counter.count++;
}
}
Counter.increment();
Counter.increment();
console.log(Counter.count);
2
Language differences
C: no static members — a file-scope static variable plays the role (single instance, with the naming prefix acting as the class name).
Go: no static members — package-level variables and functions take that role (the package itself is the scoping unit).
Java: likewise static.
JavaScript: likewise static.
PHP: static properties and methods are accessed with :: (Counter::$count outside the class, self::$count inside).
Python: uses @classmethod (first parameter cls) to operate on class attributes and @staticmethod for utility methods that touch neither instance nor class.
Rust: no static fields — conventionally associated functions (Self::new) with instances passed explicitly; global state, when truly needed, uses static plus Atomic/Mutex, but is generally discouraged.