Skip to content

Commit 08d6810

Browse files
Update Static Topic
1 parent 86152ec commit 08d6810

7 files changed

Lines changed: 171 additions & 53 deletions

File tree

docs/programming-languages/static/_csharp.mdx

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,54 @@
33
// Static Variable Inside Functions
44
// ------------------------------------
55
6-
// TODO
6+
// Not Available.
7+
// Use a static field on a class instead.
78
89
// ------------------------------------
910
// Static Member Variable Inside Class
1011
// ------------------------------------
1112
12-
// TODO
13+
class MyClass
14+
{
15+
public static int MyVariable = 0;
16+
public static readonly int MyReadonlyValue = 10;
17+
public const int MyConstant = 10;
18+
}
1319

1420
// ------------------------------------
1521
// Static Global Variable in a File
1622
// ------------------------------------
1723
18-
// TODO
24+
// No Native Global Variables.
25+
// Use a static class or static field.
1926
27+
static class Globals
28+
{
29+
public static int MyVariable = 0;
30+
}
2031

2132
// ------------------------------------
2233
// Static Functions & Methods
2334
// ------------------------------------
2435
25-
// TODO
36+
class Calculator
37+
{
38+
public static int Add(int first, int second)
39+
{
40+
return first + second;
41+
}
42+
}
2643

44+
int result = Calculator.Add(10, 20);
2745

2846
// ------------------------------------
2947
// Static Class Objects
3048
// ------------------------------------
3149
32-
// TODO
50+
static class Settings
51+
{
52+
public static string ApplicationName = "Syntax Reference";
53+
}
3354

55+
// Static classes cannot be instantiated.
3456
```

docs/programming-languages/static/_go.mdx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,51 @@
33
// Static Variable Inside Functions
44
// ------------------------------------
55

6-
// TODO
7-
6+
// Not Available.
7+
// Use package-level variables when persistent state is required.
88

99
// ------------------------------------
1010
// Static Member Variable Inside Class
1111
// ------------------------------------
1212

13-
// TODO
14-
13+
// Go has no classes and no static members.
14+
// Use package-level variables or constants.
15+
var myVariable int = 0
16+
const MY_CONSTANT int = 10
1517

1618
// ------------------------------------
1719
// Static Global Variable in a File
1820
// ------------------------------------
1921

20-
// TODO
22+
// Package-level names are shared inside the package.
23+
// Lower-case names are package-private.
24+
var myVariable int = 0
2125

26+
// Upper-case names are exported from the package.
27+
var MyVariable int = 0
2228

2329
// ------------------------------------
2430
// Static Functions & Methods
2531
// ------------------------------------
2632

27-
// TODO
33+
// Use package-level functions.
34+
func Add(first int, second int) int {
35+
return first + second
36+
}
2837

38+
result := Add(10, 20)
2939

3040
// ------------------------------------
3141
// Static Class Objects
3242
// ------------------------------------
3343

34-
// TODO
44+
// Not Available.
45+
// Use package-level variables, structs, and functions.
46+
type Settings struct {
47+
ApplicationName string
48+
}
3549

50+
var DefaultSettings = Settings{
51+
ApplicationName: "Syntax Reference",
52+
}
3653
```

docs/programming-languages/static/_java.mdx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,49 @@
33
// Static Variable Inside Functions
44
// ------------------------------------
55

6-
// TODO
7-
6+
// Not Available.
7+
// Use a static field on a class instead.
88

99
// ------------------------------------
1010
// Static Member Variable Inside Class
1111
// ------------------------------------
1212

13-
// TODO
14-
13+
class MyClass {
14+
public static int myVariable = 0;
15+
public static final int MY_CONSTANT = 10;
16+
}
1517

1618
// ------------------------------------
1719
// Static Global Variable in a File
1820
// ------------------------------------
1921

20-
// TODO
22+
// No Native Global Variables.
23+
// Use static fields on a class.
2124

25+
class Globals {
26+
public static int myVariable = 0;
27+
}
2228

2329
// ------------------------------------
2430
// Static Functions & Methods
2531
// ------------------------------------
2632

27-
// TODO
33+
class Calculator {
34+
public static int add(int first, int second) {
35+
return first + second;
36+
}
37+
}
2838

39+
int result = Calculator.add(10, 20);
2940

3041
// ------------------------------------
3142
// Static Class Objects
3243
// ------------------------------------
3344

34-
// TODO
35-
45+
// Java has static nested classes, but no top-level static classes.
46+
class Outer {
47+
static class Nested {
48+
// ...
49+
}
50+
}
3651
```

docs/programming-languages/static/_javascript.mdx

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,53 @@
33
// Static Variable Inside Functions
44
// ------------------------------------
55

6-
// TODO
6+
// No Native Support.
7+
// Use a closure when you need a function-local persistent value.
8+
const myFunction = (() => {
9+
let myVariable = 0;
710

11+
return () => {
12+
myVariable++;
13+
return myVariable;
14+
};
15+
})();
816

917
// ------------------------------------
1018
// Static Member Variable Inside Class
1119
// ------------------------------------
1220

13-
// TODO
14-
21+
class MyClass {
22+
static myVariable = 0; // Public static field
23+
}
1524

1625
// ------------------------------------
1726
// Static Global Variable in a File
1827
// ------------------------------------
1928

20-
// TODO
29+
// Use module scope.
30+
let myVariable = 0;
2131

32+
export { myVariable };
2233

2334
// ------------------------------------
2435
// Static Functions & Methods
2536
// ------------------------------------
2637

27-
// TODO
38+
class Calculator {
39+
static add(first, second) {
40+
return first + second;
41+
}
42+
}
2843

44+
const result = Calculator.add(10, 20);
2945

3046
// ------------------------------------
3147
// Static Class Objects
3248
// ------------------------------------
3349

34-
// TODO
35-
50+
// No Native Static Classes.
51+
// Use a class with static members or a plain module/object.
52+
const Settings = {
53+
applicationName: "Syntax Reference",
54+
};
3655
```

docs/programming-languages/static/_python.mdx

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,29 @@
33
# Static Variable Inside Functions
44
# ------------------------------------
55

6-
# TODO
6+
# No Native Support.
7+
# Use a function attribute if you need persistent function-local state.
8+
def my_function():
9+
if not hasattr(my_function, "counter"):
10+
my_function.counter = 0
711

12+
my_function.counter += 1
13+
return my_function.counter
814

915
# ------------------------------------
1016
# Static Member Variable Inside Class
1117
# ------------------------------------
1218

13-
# TODO
14-
19+
class MyClass:
20+
my_variable = 0
1521

1622
# ------------------------------------
1723
# Static Global Variable in a File
1824
# ------------------------------------
1925

20-
# TODO
21-
26+
# Use module-level variables.
27+
my_variable = 0
28+
MY_CONSTANT = 10
2229

2330
# ------------------------------------
2431
# Static Functions & Methods
@@ -27,9 +34,9 @@
2734
# Decorator that defines a static method
2835

2936
class Calculator:
30-
@staticmethod
31-
def add(x, y):
32-
return x + y
37+
@staticmethod
38+
def add(x, y):
39+
return x + y
3340

3441
# Calling Ex.:
3542
print('Product:', Calculator.add(15, 110))
@@ -38,9 +45,9 @@ print('Product:', Calculator.add(15, 110))
3845
# Static Class Objects
3946
# ------------------------------------
4047

41-
# TODO
48+
# Python has no static class syntax.
49+
# Use a class with class attributes, a module, or an object instance.
50+
class Settings:
51+
application_name = "Syntax Reference"
4252

4353
```
44-
45-
> More Info:
46-
> - https://realpython.com/instance-class-and-static-methods-demystified/

docs/programming-languages/static/_rust.mdx

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,52 @@
33
// Static Variable Inside Functions
44
// ------------------------------------
55

6-
// TODO
7-
6+
// No Native Function-Local Static Syntax.
7+
// Use a module-level static or a lazy initialization helper when needed.
88

99
// ------------------------------------
1010
// Static Member Variable Inside Class
1111
// ------------------------------------
1212

13-
// TODO
13+
// Rust has no classes.
14+
// Use associated constants on structs, enums, or traits.
15+
struct MyStruct;
1416

17+
impl MyStruct {
18+
const MY_CONSTANT: i32 = 10;
19+
}
1520

1621
// ------------------------------------
1722
// Static Global Variable in a File
1823
// ------------------------------------
1924

20-
// TODO
21-
25+
static MY_VARIABLE: i32 = 10;
26+
static mut MY_MUTABLE_VARIABLE: i32 = 10; // Access requires unsafe.
27+
const MY_CONSTANT: i32 = 10;
2228

2329
// ------------------------------------
2430
// Static Functions & Methods
2531
// ------------------------------------
2632

27-
// TODO
33+
struct Calculator;
34+
35+
impl Calculator {
36+
fn add(first: i32, second: i32) -> i32 {
37+
first + second
38+
}
39+
}
2840

41+
let result = Calculator::add(10, 20);
2942

3043
// ------------------------------------
3144
// Static Class Objects
3245
// ------------------------------------
3346

34-
// TODO
47+
// Rust has no classes.
48+
// Use unit structs, associated functions, modules, or static values.
49+
struct Settings;
3550

51+
impl Settings {
52+
const APPLICATION_NAME: &'static str = "Syntax Reference";
53+
}
3654
```

0 commit comments

Comments
 (0)