Skip to content

Instantly share code, notes, and snippets.

@greenrobot
Created May 3, 2017 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greenrobot/62a52d293122eab1bb05b2b659c07f74 to your computer and use it in GitHub Desktop.
Save greenrobot/62a52d293122eab1bb05b2b659c07f74 to your computer and use it in GitHub Desktop.
(Almost) Circular class dependencies in Java. What does this print?
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