- package cn.heima;
- class A {
- private static A a = new A();
- private B b = B.getInstance();
- private A() {
- }
- public static A getInstance() {
- System.out.println("A.getInstance返回的a是:" + a);
- return a;
- }
- public void test() {
- System.out.println("A中test:a = "+ a);
- System.out.println("A中test:b = "+ b);
- }
- }
- class B {
- private static B b = new B();
- private A a = A.getInstance();
- private B() {
- }
- public static B getInstance() {
- System.out.println("B.getInstance返回的b是:" + b);
- return b;
- }
- public void test() {
- System.out.println("B中test:a = " + a);
- System.out.println("B中test:b = " + b);
- }
- }
- class Test {
- public static void main(String[] args) {
- A testa = A.getInstance();
- B testb = B.getInstance();
- System.out.println("==========");
- testa.test();
- testb.test();
- }
- }
复制代码 打印结果是:
A.getInstance返回的a是:null
B.getInstance返回的b是:cn.heima.B@1fb8ee3
A.getInstance返回的a是:cn.heima.A@61de33
B.getInstance返回的b是:cn.heima.B@1fb8ee3
==========
A中test:a = cn.heima.A@61de33
A中test:b = cn.heima.B@1fb8ee3
B中test:a = null
B中test:b = cn.heima.B@1fb8ee3
问题是那个a 为什么是null 呢
|