Skip to content

Commit f7edd9b

Browse files
Static class example (review comments addressed)
1 parent 5d545fb commit f7edd9b

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

  • core-java/src/main/java/com/baeldung/staticclass
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.staticclass;
2+
3+
public class Pizza {
4+
5+
private static String cookedCount;
6+
private boolean isThinCrust;
7+
8+
// Accessible globally
9+
public static class PizzaSalesCounter {
10+
11+
private static String orderedCount;
12+
public static String deliveredCount;
13+
14+
PizzaSalesCounter() {
15+
System.out.println("Static field of enclosing class is "
16+
+ Pizza.cookedCount);
17+
System.out.println("Non-static field of enclosing class is "
18+
+ new Pizza().isThinCrust);
19+
}
20+
}
21+
22+
Pizza() {
23+
System.out.println("Non private static field of static class is "
24+
+ PizzaSalesCounter.deliveredCount);
25+
System.out.println("Private static field of static class is "
26+
+ PizzaSalesCounter.orderedCount);
27+
}
28+
29+
public static void main(String[] a) {
30+
// Create instance of the static class without an instance of enclosing class
31+
new Pizza.PizzaSalesCounter();
32+
}
33+
}

0 commit comments

Comments
 (0)