If i create an object in Class1 and save some data in that object in Class1, how can i access that same data in Class2? Here's a code sample that should clarify my situation:

Class1.java:

public class Class1 implements Runnable {
    public ClientArray ca;

        public Class1(int port) throws IOException {
        ca = new ClientArray();
    Client client = new Client(socket);
    ca.setArraySize();
    ca.addClient(client);
    }

Class2.java:

    protected void doInSession() {


    //need to access that same ClientArray ca here that i created in Class1!
        System.out.println(ca.getClient() + "clietttttttttttttt");
    }


ClientArray.java:

public class ClientArray {
    Client[] ClientArray;

    public void ClientArray() {
    }

    public void setArraySize() {
        System.out.println("new ca");
        ClientArray = new Client[100];
    }


    public void addClient(Client client) {
        ClientArray[0] = client;

    }
    public Client getClient() {
        System.out.println(ClientArray[0].getSocket() + " aaaaaaaaaaaaaaaaaa");
        return ClientArray[0];
    }

}

Dani AI

Generated

Three practical, safe ways to share the same ClientArray instance between classes — ranked by preference and with short examples and cautions.

  • Prefer passing the instance explicitly (constructor or setter). It keeps code testable and avoids hidden globals. This fits ’s suggestion (return ca from Class1) but goes one step further: give Class2 a reference to the ClientArray directly instead of reaching into Class1.
public class Class2 {
    private final ClientArray ca;
    public Class2(ClientArray ca) { this.ca = ca; }
    protected void doInSession() {
        Client client = ca.getClient(); // check for null before use
        System.out.println(client);
    }
}
  • If you must have a process-wide single store, prefer a thread-safe singleton pattern over ad-hoc static fields. The enum-singleton is simple and inherently safe:
public enum ClientRegistry {
    INSTANCE;
    private final List<Client> clients = new CopyOnWriteArrayList<>();
    public void add(Client c) { clients.add(c); }
    public Client get(int i) { return clients.get(i); }
}

Use with ClientRegistry.INSTANCE.add(client) and ClientRegistry.INSTANCE.get(0) from any class. This is a safer alternative to the basic lazy-getInstance shown earlier.

  • Static references on Class1 (as suggested by ) work, but they couple code tightly and make testing and reuse harder. Use them only when globals are unavoidable.

A few important notes specific to the posted code (from @C:>): the ClientArray class appears to have an incorrectly declared constructor (a method with a return type). Fix it to a proper constructor so objects initialize as expected. Also prefer a List or concurrent collection (CopyOnWriteArrayList / ConcurrentLinkedQueue / ConcurrentHashMap) over a bare array for a dynamic client list, and protect mutations if multiple threads will add/remove clients.

Recommended Answers

All 6 Replies

Add a method in Class1 that returns ca to its caller.
What is the relationship between Class1 and Class2? Does either have a reference to the other?

No they don't have reference, and i don't understand how could i call that method from Class1, since i would have to access that same instance of Class1 where ca is created.

It looks like i can achieve what i want by creating ClientArray like this:

   ca = ClientArray.getInstance();

and getInstance looks like this:

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

Making it static means there is only ONE instance of the ClientArray class to be accessed.

I don't think that will be a problem, since ClientArray class is only supposed to be a sort of "database" of currently connected clients.

Define a static Class1 object and inside the constructor's declaration for Class1 assign this to the static object.Then you can access the Class1 object by simply calling Class1.<static_obj_ref_name>.<method_name()>/<var_name>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.