Created
May 3, 2017 15:49
-
-
Save greenrobot/62a52d293122eab1bb05b2b659c07f74 to your computer and use it in GitHub Desktop.
(Almost) Circular class dependencies in Java. What does this print?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.greenrobot; | |
import java.util.concurrent.atomic.AtomicInteger; | |
public class CircularStaticDependencies { | |
static AtomicInteger count = new AtomicInteger(1); | |
static class A { | |
static String a1 = "a" + count.getAndIncrement(); | |
static String a2 = "a" + count.getAndIncrement() + B.b1; | |
static String a3 = "a" + count.getAndIncrement() + B.b2; | |
} | |
static public class B { | |
static String b1 = "b" + count.getAndIncrement(); | |
static String b2 = "b" + count.getAndIncrement() + A.a1; | |
static String b3 = "b" + count.getAndIncrement() + A.a2; | |
} | |
public static void main(String[] args) { | |
System.out.println("a1, a2, a3: " + A.a1 + ", " + A.a2 + ", " + A.a3); | |
System.out.println("b1, b2, b3: " + B.b1 + ", " + B.b2 + ", " + B.b3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment