Static
- C
- C++
- C#
- Java
- Rust
- Go
- Javascript
- Typescript
- Python
// ------------------------------------
// Static Variable Inside Functions
// ------------------------------------
// When the variable is scoped and marked as static,
// their value will be preserved in memory even after
// they are out of their scope.
void myFunction()
{
static int myVariable = 0; // myVariable will keep its value for every call
// ...
}
// ------------------------------------
// Static Member Variable Inside Class
// ------------------------------------
// Not Available
// C has no Class
// ------------------------------------
// Static Global Variable in a File
// ------------------------------------
// When the variable is declared in the global scope (outside any function),
// their access will be restricted to the file itself.
// Wihtout "static", all global variables are globally accessible by default
// The purpose is to limit scope of a global variable to a file.
static int myVariable = 0;
// ------------------------------------
// Static Functions & Methods
// ------------------------------------
// In C, static functions are restricted to the file where they are declared.
// Wihtout "static", all functions are globally accessible by default
// The purpose is to limit scope of a function to a file.
static void myFunction() // This functions is scoped only accessible internally
{
// ...
}
// ------------------------------------
// Static Class Objects
// ------------------------------------
// Not Available
// C has no Class
// ------------------------------------
// Static Variable Inside Functions
// ------------------------------------
// When the variable is scoped and marked as static,
// their value will be preserved in memory even after
// they are out of their scope.
void myFunction()
{
static int myVariable = 0; // myVariable will keep its value for every call
// ...
}
// ------------------------------------
// Static Member Variable Inside Class
// ------------------------------------
// Static member variables are shared by all the objects of a class
// They must be initialized explicitly. (Not initialied using constructor)
class MyClass
{
public:
static int myVariable = 0;
// ...
};
// ------------------------------------
// Static Global Variable in a File
// ------------------------------------
// When the variable is declared in the global scope (outside any function),
// their access will be restricted to the file itself.
// Wihtout "static", all global variables are globally accessible by default
// The purpose is to limit scope of a global variable to a file.
static int myVariable = 0;
// ------------------------------------
// Static Functions & Methods
// ------------------------------------
// Static methods (member functions) are shared by all the objects of a class
class MyClass
{
public:
static int myFunction();
// ...
};
// ------------------------------------
// Static Class Objects
// ------------------------------------
// The scope of static object is through out the life time of program.
static MyClass myObject;
// ------------------------------------
// Static Variable Inside Functions
// ------------------------------------
// Not Available.
// Use a static field on a class instead.
// ------------------------------------
// Static Member Variable Inside Class
// ------------------------------------
class MyClass
{
public static int MyVariable = 0;
public static readonly int MyReadonlyValue = 10;
public const int MyConstant = 10;
}
// ------------------------------------
// Static Global Variable in a File
// ------------------------------------
// No Native Global Variables.
// Use a static class or static field.
static class Globals
{
public static int MyVariable = 0;
}
// ------------------------------------
// Static Functions & Methods
// ------------------------------------
class Calculator
{
public static int Add(int first, int second)
{
return first + second;
}
}
int result = Calculator.Add(10, 20);
// ------------------------------------
// Static Class Objects
// ------------------------------------
static class Settings
{
public static string ApplicationName = "Syntax Reference";
}
// Static classes cannot be instantiated.
// ------------------------------------
// Static Variable Inside Functions
// ------------------------------------
// Not Available.
// Use a static field on a class instead.
// ------------------------------------
// Static Member Variable Inside Class
// ------------------------------------
class MyClass {
public static int myVariable = 0;
public static final int MY_CONSTANT = 10;
}
// ------------------------------------
// Static Global Variable in a File
// ------------------------------------
// No Native Global Variables.
// Use static fields on a class.
class Globals {
public static int myVariable = 0;
}
// ------------------------------------
// Static Functions & Methods
// ------------------------------------
class Calculator {
public static int add(int first, int second) {
return first + second;
}
}
int result = Calculator.add(10, 20);
// ------------------------------------
// Static Class Objects
// ------------------------------------
// Java has static nested classes, but no top-level static classes.
class Outer {
static class Nested {
// ...
}
}
// ------------------------------------
// Static Variable Inside Functions
// ------------------------------------
// No Native Function-Local Static Syntax.
// Use a module-level static or a lazy initialization helper when needed.
// ------------------------------------
// Static Member Variable Inside Class
// ------------------------------------
// Rust has no classes.
// Use associated constants on structs, enums, or traits.
struct MyStruct;
impl MyStruct {
const MY_CONSTANT: i32 = 10;
}
// ------------------------------------
// Static Global Variable in a File
// ------------------------------------
static MY_VARIABLE: i32 = 10;
static mut MY_MUTABLE_VARIABLE: i32 = 10; // Access requires unsafe.
const MY_CONSTANT: i32 = 10;
// ------------------------------------
// Static Functions & Methods
// ------------------------------------
struct Calculator;
impl Calculator {
fn add(first: i32, second: i32) -> i32 {
first + second
}
}
let result = Calculator::add(10, 20);
// ------------------------------------
// Static Class Objects
// ------------------------------------
// Rust has no classes.
// Use unit structs, associated functions, modules, or static values.
struct Settings;
impl Settings {
const APPLICATION_NAME: &'static str = "Syntax Reference";
}
// ------------------------------------
// Static Variable Inside Functions
// ------------------------------------
// Not Available.
// Use package-level variables when persistent state is required.
// ------------------------------------
// Static Member Variable Inside Class
// ------------------------------------
// Go has no classes and no static members.
// Use package-level variables or constants.
var myVariable int = 0
const MY_CONSTANT int = 10
// ------------------------------------
// Static Global Variable in a File
// ------------------------------------
// Package-level names are shared inside the package.
// Lower-case names are package-private.
var myVariable int = 0
// Upper-case names are exported from the package.
var MyVariable int = 0
// ------------------------------------
// Static Functions & Methods
// ------------------------------------
// Use package-level functions.
func Add(first int, second int) int {
return first + second
}
result := Add(10, 20)
// ------------------------------------
// Static Class Objects
// ------------------------------------
// Not Available.
// Use package-level variables, structs, and functions.
type Settings struct {
ApplicationName string
}
var DefaultSettings = Settings{
ApplicationName: "Syntax Reference",
}
// ------------------------------------
// Static Variable Inside Functions
// ------------------------------------
// No Native Support.
// Use a closure when you need a function-local persistent value.
const myFunction = (() => {
let myVariable = 0;
return () => {
myVariable++;
return myVariable;
};
})();
// ------------------------------------
// Static Member Variable Inside Class
// ------------------------------------
class MyClass {
static myVariable = 0; // Public static field
}
// ------------------------------------
// Static Global Variable in a File
// ------------------------------------
// Use module scope.
let myVariable = 0;
export { myVariable };
// ------------------------------------
// Static Functions & Methods
// ------------------------------------
class Calculator {
static add(first, second) {
return first + second;
}
}
const result = Calculator.add(10, 20);
// ------------------------------------
// Static Class Objects
// ------------------------------------
// No Native Static Classes.
// Use a class with static members or a plain module/object.
const Settings = {
applicationName: "Syntax Reference",
};
// ------------------------------------
// Static Variable Inside Functions
// ------------------------------------
// No Native Support.
// Use a closure when you need a function-local persistent value.
const myFunction = (() => {
let myVariable: number = 0;
return (): number => {
myVariable++;
return myVariable;
};
})();
// ------------------------------------
// Static Member Variable Inside Class
// ------------------------------------
class MyClass {
public static myVariable: number = 0;
public static readonly myReadonlyValue: number = 10;
}
// ------------------------------------
// Static Global Variable in a File
// ------------------------------------
// Use module scope.
let myVariable: number = 0;
export { myVariable };
// ------------------------------------
// Static Functions & Methods
// ------------------------------------
class Calculator {
public static add(first: number, second: number): number {
return first + second;
}
}
const result: number = Calculator.add(10, 20);
// ------------------------------------
// Static Class Objects
// ------------------------------------
// No Native Static Classes.
// Use a class with static members or a plain module/object.
const Settings = {
applicationName: "Syntax Reference",
};
# ------------------------------------
# Static Variable Inside Functions
# ------------------------------------
# No Native Support.
# Use a function attribute if you need persistent function-local state.
def my_function():
if not hasattr(my_function, "counter"):
my_function.counter = 0
my_function.counter += 1
return my_function.counter
# ------------------------------------
# Static Member Variable Inside Class
# ------------------------------------
class MyClass:
my_variable = 0
# ------------------------------------
# Static Global Variable in a File
# ------------------------------------
# Use module-level variables.
my_variable = 0
MY_CONSTANT = 10
# ------------------------------------
# Static Functions & Methods
# ------------------------------------
# Decorator that defines a static method
class Calculator:
@staticmethod
def add(x, y):
return x + y
# Calling Ex.:
print('Product:', Calculator.add(15, 110))
# ------------------------------------
# Static Class Objects
# ------------------------------------
# Python has no static class syntax.
# Use a class with class attributes, a module, or an object instance.
class Settings:
application_name = "Syntax Reference"