Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share data between separate classes in Java

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 me try to illustrate a simplified version of my problem:

This was my code before:

public class Top_Level_Class(){
    int x, y;

    // gets user input which changes x, y;
    public void main(){
       int p, q, r, s;
       // compute p, q, r, s
       doA(p,q,r);
       doB(q,r,s);
    }

    public void doA(int p, int q, int r){
       // do something that requires x,y and p, q, r
    }

    public void doB(int q, int r, int s){
       // does something else that requires x, y and q, r, s
    }
}

Now it looks something like this:

public class Top_Level_Class(){
    int x, y;
    SomeClass1a a = new SomeClass1a();
    SomeClass1a b = new SomeClass1b();
    // gets user input which changes x, y;
    public void main(){
       int p, q, r, s;
       // compute p, q, r, s
       a.doA(p,q,r);
       b.doB(q,r,s);
    }

public class SomeClass1a() {  // in its own separate file
    public void doA(int p, int q, int r){
       // do something that requires x,y and p, q, r
    }
}


public class SomeClass1b() {  // in its own separate file
    public void doB(int q, int r, int s){
       // does something else that requires x, y and q, r, s
    }
}

So anyway, should I pass x and y each time (where x,y are variables stored in the helper class func) ?

 a.set(x,y);
 a.doA(p,q,r);

My idea was to have a special container class where x and y are held. The top level class would have an instance of the container class and change x,y using the set methods.

// in the top level class:
Container c = new Container(x,y);
a.setContainer(c);
b.setContainer(c);

My helper classes would also have an instance of the container and it would point to the same instance as in the top level. That way they access the same x,y as in the top level.

I would like to know if I should

  • Use the container class
  • Load x,y each time into the subclasses
  • ?? Some better method ??
like image 295
f20k Avatar asked Dec 12 '10 00:12

f20k


People also ask

How do you communicate between classes in Java?

In java we can call the members of one class from another class by creating an object. It is useful when we need to use common code in every class again and again. This is called communication between two classes and can be done in more than one way. You can also say that these objects can talk to each other.

How do you share a class in Java?

Let's suppose classes. jsa file is not present, you can create it yourself using the command java -Xshare:dump. This command will go to classlist file and check what classes to load and create a dump of it. One can try deleting the jre/bin/[client or server]/classes.

How do you pass data between objects in Java?

GroupChat chat = new GroupChat(); you are creating new object with default values (e.g. 0, nulls). If you want to use your object "B" you have to return it from the function where you call setters.

Can we create separate classes as separate files in Java?

Of course you can separate out 2 classes and still they will work fine. Just make sure you import one class in another using import statement. All you need to do is move AnotherClass class into a separate source file named with name same as class name ie "AnotherClass.


1 Answers

I guess the answer to your question is the Design Pattern called Singleton. It basically allows you to get and exploits the same (and unique) instance of a class whenever you want in your system.

This is its implementation (please forgive possible syntax errors, I did not compile it):

class Container{

  //eventually provides setters and getters
  public float x;
  public float y;
  //------------

  private static Container instance = null;
  private void Container(){

  }
  public static Container getInstance(){
    if(instance==null){
       instance = new Container();
      }
      return instance;
  }
}

then if elsewhere in your code you import the Container you can write for example

Container.getInstance().x = 3;
temp = Container.getInstance().x;

and you will affect the attributes of the unique container instance you have in your system

In many cases it is however better to use the Dependency Injection pattern as it reduces the coupling between different components.

like image 112
Andrea Sindico Avatar answered Sep 21 '22 09:09

Andrea Sindico