How to share data between separate classes in Java

后端 未结 4 951
面向向阳花
面向向阳花 2020-12-29 12:44

What is the best way to share data between separate classes in Java? I have a bunch of variables that are used by different classes in separate files in different ways. Let

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-29 13:24

    This question is a bit mad - the code in the question won't compile.

    public class Main {
        public static void main(String... args) {
            MutableDataContainer m = new MutableDataContainer();
            ImmutableDataContainer i = computeImmutableData();
            new ADoer().doA(m, i);
            new BDoer().doB(m, i);
        }
        ...
    }
    
    class MutableDataContainer {
        private int x, y;
        ... // getters and setters below
    }
    
    class ImmutableDataContainer {
        private final int p, q, r, s;
        ... // getters below
    }
    

    You'll need to define ADoer and BDoer as well.

提交回复
热议问题