Skip to content

Commit 025fd20

Browse files
committed
feat: Add StaticTheory component providing a deep dive into static vs non-static concepts.
1 parent a70ec08 commit 025fd20

File tree

4 files changed

+363
-0
lines changed

4 files changed

+363
-0
lines changed

src/features/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export { GCModule } from './gc';
22
export { ThreadsModule } from './threads';
33
export { JITModule } from './jit';
44
export { JMMModule } from './jmm';
5+
export { StaticModule } from './static';
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
.container {
2+
flex: 1;
3+
overflow-y: auto;
4+
background-color: var(--color-bg-primary);
5+
padding: var(--space-8);
6+
}
7+
8+
.content {
9+
max-width: 800px;
10+
margin: 0 auto;
11+
display: flex;
12+
flex-direction: column;
13+
gap: var(--space-8);
14+
}
15+
16+
.title {
17+
font-size: var(--text-3xl);
18+
font-weight: 700;
19+
color: var(--color-text-primary);
20+
}
21+
22+
.card {
23+
background-color: var(--color-bg-secondary);
24+
padding: var(--space-6);
25+
border-radius: var(--radius-lg);
26+
border-left: 4px solid;
27+
}
28+
29+
.pink {
30+
border-color: #f472b6;
31+
}
32+
33+
.green {
34+
border-color: var(--color-success);
35+
}
36+
37+
.red {
38+
border-color: var(--color-error);
39+
}
40+
41+
.yellow {
42+
border-color: var(--color-warning);
43+
}
44+
45+
.purple {
46+
border-color: var(--color-jit-primary);
47+
}
48+
49+
.blue {
50+
border-color: var(--color-info);
51+
}
52+
53+
.indigo {
54+
border-color: #818cf8;
55+
}
56+
57+
.cardTitle {
58+
font-size: var(--text-xl);
59+
font-weight: 700;
60+
color: var(--color-text-primary);
61+
margin-bottom: var(--space-3);
62+
}
63+
64+
.cardText {
65+
color: var(--color-text-secondary);
66+
line-height: 1.7;
67+
margin-bottom: var(--space-3);
68+
}
69+
70+
.cardText code,
71+
.list code,
72+
.smallList code {
73+
background-color: var(--color-bg-tertiary);
74+
padding: 2px 6px;
75+
border-radius: var(--radius-sm);
76+
font-family: var(--font-mono);
77+
font-size: var(--text-sm);
78+
}
79+
80+
.list {
81+
color: var(--color-text-secondary);
82+
padding-left: var(--space-5);
83+
display: flex;
84+
flex-direction: column;
85+
gap: var(--space-3);
86+
line-height: 1.6;
87+
}
88+
89+
.smallList {
90+
color: var(--color-text-secondary);
91+
padding-left: var(--space-4);
92+
display: flex;
93+
flex-direction: column;
94+
gap: var(--space-2);
95+
font-size: var(--text-sm);
96+
}
97+
98+
.memoryDiagram {
99+
display: flex;
100+
gap: var(--space-4);
101+
align-items: center;
102+
flex-wrap: wrap;
103+
margin-top: var(--space-4);
104+
}
105+
106+
.memoryBox {
107+
background-color: #000;
108+
border: 2px solid var(--color-border-default);
109+
border-radius: var(--radius-lg);
110+
padding: var(--space-3);
111+
min-width: 200px;
112+
}
113+
114+
.memoryLabel {
115+
font-size: var(--text-xs);
116+
color: #f472b6;
117+
font-weight: 700;
118+
text-transform: uppercase;
119+
margin-bottom: var(--space-2);
120+
}
121+
122+
.memoryContent {
123+
font-family: var(--font-mono);
124+
font-size: var(--text-sm);
125+
color: var(--color-text-secondary);
126+
line-height: 1.6;
127+
}
128+
129+
.arrow {
130+
font-size: var(--text-2xl);
131+
color: var(--color-text-muted);
132+
}
133+
134+
.codeBlock {
135+
background-color: #000;
136+
padding: var(--space-4);
137+
border-radius: var(--radius-md);
138+
font-family: var(--font-mono);
139+
font-size: 12px;
140+
color: var(--color-text-secondary);
141+
overflow-x: auto;
142+
margin: 0;
143+
line-height: 1.5;
144+
}
145+
146+
.cardNote {
147+
margin-top: var(--space-3);
148+
font-size: var(--text-sm);
149+
color: var(--color-text-muted);
150+
font-style: italic;
151+
}
152+
153+
.twoColumn {
154+
display: grid;
155+
grid-template-columns: 1fr 1fr;
156+
gap: var(--space-6);
157+
}
158+
159+
@media (max-width: 600px) {
160+
.twoColumn {
161+
grid-template-columns: 1fr;
162+
}
163+
}
164+
165+
.subTitle {
166+
font-size: var(--text-base);
167+
font-weight: 600;
168+
color: var(--color-text-primary);
169+
margin-bottom: var(--space-2);
170+
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/**
2+
* Static Theory View
3+
* Comprehensive educational content about static vs instance members
4+
*/
5+
6+
import styles from './StaticTheory.module.css';
7+
8+
export function StaticTheory() {
9+
return (
10+
<div className={styles.container}>
11+
<div className={styles.content}>
12+
<h2 className={styles.title}>Static vs Non-Static: Deep Dive</h2>
13+
14+
{/* Under the Hood Section */}
15+
<div className={`${styles.card} ${styles.pink}`}>
16+
<h3 className={styles.cardTitle}>🔧 Under the Hood: JVM Memory</h3>
17+
<p className={styles.cardText}>
18+
When the JVM loads a class, it creates a <strong>Class Object</strong> in
19+
Metaspace (formerly PermGen). This is where <code>static</code> members live.
20+
</p>
21+
<div className={styles.memoryDiagram}>
22+
<div className={styles.memoryBox}>
23+
<div className={styles.memoryLabel}>Metaspace</div>
24+
<div className={styles.memoryContent}>
25+
<code>Counter.class</code><br />
26+
<code>static totalCount = 5</code><br />
27+
<code>static getTotalCount()</code>
28+
</div>
29+
</div>
30+
<div className={styles.arrow}></div>
31+
<div className={styles.memoryBox}>
32+
<div className={styles.memoryLabel}>Heap</div>
33+
<div className={styles.memoryContent}>
34+
<code>Counter@0x001</code><br />
35+
<code>instanceId = 1</code><br />
36+
<code>__class → Counter.class</code>
37+
</div>
38+
</div>
39+
</div>
40+
</div>
41+
42+
{/* When to Use Static */}
43+
<div className={`${styles.card} ${styles.green}`}>
44+
<h3 className={styles.cardTitle}>✅ When to Use Static</h3>
45+
<ul className={styles.list}>
46+
<li>
47+
<strong>Utility Methods:</strong> <code>Math.abs()</code>, <code>Collections.sort()</code> -
48+
No object state needed, stateless operations
49+
</li>
50+
<li>
51+
<strong>Constants:</strong> <code>static final int MAX_SIZE = 100</code> -
52+
Values that never change
53+
</li>
54+
<li>
55+
<strong>Counters/IDs:</strong> Track total instances created
56+
</li>
57+
<li>
58+
<strong>Factory Methods:</strong> <code>LocalDate.now()</code>, <code>Optional.of()</code>
59+
</li>
60+
<li>
61+
<strong>Singletons:</strong> Single shared instance pattern
62+
</li>
63+
<li>
64+
<strong>Caching:</strong> Class-level caches shared across all instances
65+
</li>
66+
</ul>
67+
</div>
68+
69+
{/* When NOT to Use Static */}
70+
<div className={`${styles.card} ${styles.red}`}>
71+
<h3 className={styles.cardTitle}>❌ When NOT to Use Static</h3>
72+
<ul className={styles.list}>
73+
<li>
74+
<strong>Object State:</strong> Each instance needs its own copy of the data
75+
</li>
76+
<li>
77+
<strong>Polymorphism Needed:</strong> Static methods cannot be overridden
78+
(only hidden). Use instance methods for strategy pattern.
79+
</li>
80+
<li>
81+
<strong>Testing:</strong> Static state persists between tests, causing flaky tests
82+
</li>
83+
<li>
84+
<strong>Thread Safety:</strong> Shared mutable static state requires synchronization
85+
</li>
86+
<li>
87+
<strong>Dependency Injection:</strong> Static methods can't use injected dependencies
88+
</li>
89+
</ul>
90+
</div>
91+
92+
{/* Thread Safety Warning */}
93+
<div className={`${styles.card} ${styles.yellow}`}>
94+
<h3 className={styles.cardTitle}>⚠️ Static & Thread Safety</h3>
95+
<p className={styles.cardText}>
96+
Static fields are shared across <strong>ALL threads</strong>. Without synchronization:
97+
</p>
98+
<pre className={styles.codeBlock}>
99+
{`// DANGEROUS - Race condition!
100+
public class Counter {
101+
private static int count = 0;
102+
103+
public static void increment() {
104+
count++; // NOT atomic!
105+
}
106+
}
107+
108+
// SAFE - Use AtomicInteger
109+
private static AtomicInteger count = new AtomicInteger(0);`}
110+
</pre>
111+
</div>
112+
113+
{/* Static Binding */}
114+
<div className={`${styles.card} ${styles.purple}`}>
115+
<h3 className={styles.cardTitle}>🔗 Static Binding (Compile-Time)</h3>
116+
<p className={styles.cardText}>
117+
Static methods are resolved at <strong>compile time</strong>, not runtime.
118+
This means polymorphism doesn't work:
119+
</p>
120+
<pre className={styles.codeBlock}>
121+
{`class Animal {
122+
static void speak() { System.out.println("Animal"); }
123+
}
124+
class Dog extends Animal {
125+
static void speak() { System.out.println("Dog"); }
126+
}
127+
128+
Animal a = new Dog();
129+
a.speak(); // Prints "Animal", NOT "Dog"!
130+
// Resolved by reference type, not object type`}
131+
</pre>
132+
</div>
133+
134+
{/* Best Practices */}
135+
<div className={`${styles.card} ${styles.blue}`}>
136+
<h3 className={styles.cardTitle}>💡 Best Practices</h3>
137+
<div className={styles.twoColumn}>
138+
<div>
139+
<h4 className={styles.subTitle}>Do</h4>
140+
<ul className={styles.smallList}>
141+
<li>Use <code>static final</code> for constants</li>
142+
<li>Make utility classes have private constructor</li>
143+
<li>Use static factory methods when appropriate</li>
144+
<li>Document thread-safety guarantees</li>
145+
</ul>
146+
</div>
147+
<div>
148+
<h4 className={styles.subTitle}>Don't</h4>
149+
<ul className={styles.smallList}>
150+
<li>Use static mutable state liberally</li>
151+
<li>Use static to avoid passing parameters</li>
152+
<li>Overuse static for "convenience"</li>
153+
<li>Mix static initialization with side effects</li>
154+
</ul>
155+
</div>
156+
</div>
157+
</div>
158+
159+
{/* Static Initialization */}
160+
<div className={`${styles.card} ${styles.indigo}`}>
161+
<h3 className={styles.cardTitle}>📦 Static Initialization Order</h3>
162+
<pre className={styles.codeBlock}>
163+
{`class Example {
164+
// 1. Static fields initialized first (in order)
165+
static int x = 10;
166+
167+
// 2. Static block runs after fields
168+
static {
169+
System.out.println("Static block: x = " + x);
170+
x = 20;
171+
}
172+
173+
// 3. Instance initialization happens on new
174+
int y = 30;
175+
{ System.out.println("Instance block"); }
176+
177+
// 4. Constructor runs last
178+
Example() { System.out.println("Constructor"); }
179+
}`}
180+
</pre>
181+
<p className={styles.cardNote}>
182+
Static initialization happens <strong>once</strong> when class is first loaded.
183+
</p>
184+
</div>
185+
186+
</div>
187+
</div>
188+
);
189+
}

src/features/static/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { StaticModule } from './StaticModule';
2+
export { StaticSimulator } from './StaticSimulator';
3+
export { StaticTheory } from './StaticTheory';

0 commit comments

Comments
 (0)