简体   繁体   中英

Can I compile two interdependent class in Java?

I have two files, Utils.java and Gate.java. for example

Utils.java

package yao;
import yao.gate.Gate;

Gate.java

package yao.gate;
import yao.Utils;

Gate class uses function of Utils class and Utills Class uses function of Gate class. Can I compile these two interdependent files, or should I consider changing my code design? If possible, then please tell me how can I do that?

It is possible. However, this does not sound a good design. Create a new class and move the common code to that class and remove the interdependency between the two classes.

As @Vlaxmi mentionned, there might be cases where it would not compile depending on how one code calls another. But there are at least two scenarios where it compiles (without compilation error). Assuming I have two classes A and B in a similar package configuration as the one you described.

  • Case1: A method from an instance of class A invokes a method from an instance of class B and another method from class B invokes a different method from class A. In this case compilation will be fine as well as execution

  • Case2: A method from an instance of a class C invokes a method from an instance of class B and the same instance method from class B invokes in its turn the same instance method from class C. The compilation will work fine, but the execution will end up with a StackOverflowError.

Here is the illustration in code

package interdependency;

import interdependency.sub.*;
public class A {
    public void aM1() {
        System.out.println("In a.M1 method");
    }

    public void aM2() {
        System.out.println("In a.M2 method");
        B b = new B();
        b.bM2();
    }
}

Here is a class B:

package interdependency.sub;

import interdependency.A;

public class B {
    public void bM1() {
        System.out.println("In bM1 method:");
        A a = new A();
        a.aM1();
    }

    public void bM2() {
        System.out.println("In bM2 method");
    }
}

Using a simple class to run this scenario

package interdependency;

import interdependency.sub.B;

public class InterdependencyTester {
    public static void main(String[] args) {
        A a = new A();
        B b = new B();
        a.aM1();
        a.aM2();
        b.bM1();
        b.bM2();
        C c = new C();
        c.cM1();
    }
}

In this current configuration compilation as well as execution both work fine. Here is the second scenario where things don't go so well, but where compilation still runs fine.

Let's assume a certain class C similar to the two others:

package interdependency;

import interdependency.sub.B;

public class C {
    public void cM1() {
        System.out.println("inside cM1");
        B b = new B();
        b.bM3();    
    }
}

And now let's update B with a third method bM3

package interdependency.sub;

import interdependency.A;
import interdependency.C;

public class B {
    public void bM1() {
        System.out.println("In bM1 method:");
        A a = new A();
        a.aM1();
    }

    public void bM2() {
        System.out.println("In bM2 method");
    }

    public void bM3() {
        System.out.println("In bM2 method");
        C c = new C();
        c.cM1();
    }
}

This time C.cM1 calls B.bM3 and B.bM3 calls C.cM1. It compiles but we end up with a stack overflow at runtime

在此处输入图片说明

Two interdependent classes can compile depending on what level of complexity it has. eg It is not a good design to have two classes with methods calling each other. That would result in deadlock code. eg Utils.java

package yao;

import yao.gate.Gate;

public class Utils {
    Gate gate = new Gate();
    public void testUtils(){
        System.out.println("testUtils");
        gate.gateMethod();
    }
}

Gate.java

package yao.gate;

import yao.Utils;


public class Gate {
    Utils util = new Utils();
    public void gateMethod(){
        util.testUtils();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM