user3431664
user3431664

Reputation: 51

creating an object that shares its property among classes

I would like to make an object of a class A and it is initialized in class B, class C, and class D

This object should be shared; that is if any changes made to objA in these classes(C and D), its content remains the same even after objC,'objD' are destroyed, supposely that class B is the main class. I would like to use its property is class B

class A {}

class B
{
  initialize class A object and use-change its property
  initialize class C object and use-change its property
  initialize class D object and use-change its property
}

class C{initialize class A object and use-change its property}

class D{initialize class A object and use-change its property}

class X{initialize B and destroy objC,objD, from objB use property of objA of class B}

Upvotes: 0

Views: 63

Answers (3)

Rogue
Rogue

Reputation: 11483

This is essentially a singleton pattern, you can pass your class around or get it from the main. I prefer the latter personally, like so:

public class YourMain {

    private final ClassA clazzA;
    private final ClassB clazzB;
    private final ClassC clazzC;
    private final ClassD clazzD;


    public YourMain() {
        this.clazzA = new ClassA();
        this.clazzB = new ClassB(this);
        this.clazzC = new ClassC(this);
        this.clazzD = new ClassD(this);


        this.clazzB.doSomething();
    }

    public ClassA getClassA() {
        return this.clazzA;
    }

}

From there, you can chain back up to your main class in the other classes:

public class ClassB {

    private final YourMain project;

    public ClassB(YourMain project) {
        this.project = project;
    }

    public void doSomething() {
        this.project.getClassA().someMethod();
    }

}

Of course, this isn't 100% what you would implement it like (you need to mind the order you load things in if you are passing your main class instance), but for something like this I usually find it is the cleanest and provides the easiest availability to all my classes across a project.

Upvotes: 0

ifloop
ifloop

Reputation: 8386

A non static attempt would look like this:

Your target object:

public class A {

}

Your classes, that do something with the object:

public class C {
    private final A a;

    public C(final A a) {
        this.a = a;
    }

    public foo() {
        // do something with a
    }
}

public class D {
    private final A a;

    public D(final A a) {
        this.a = a;
    }

    public otherFoo() {
        // do something with a
    }
}

Your main class:

public class B {

    public static void main(String[] args) {
        final A a = new A();

        final C c = new C(a);
        final D d = new D(a);


        c.foo();
        d.otherFoo();
    }
}

Upvotes: 1

Anthony_Michael
Anthony_Michael

Reputation: 42

Try making the object static that way any changes made to one instance of that object will be consistent across all objects that use that object that are in the same thread.

Upvotes: 0

Related Questions