Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(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