WilliamShatner
WilliamShatner

Reputation: 926

How do you pass instances created in main to classes?

First, I apologize if this is a dumb question. This has been bothering me recently and I figured it was worth asking...

I have an application where the classes flow like so:

enter image description here

I'm currently trying to allow my Client class and Server class to have access to instances of each other that are created in MyApp's main. The issue is that the instances are created in main (thus making them static) and I'm wondering how to properly pass them to the other classes as what I've done doesn't seem like it is the right way.

Here is what I've done:

public class MyApp {
    private static RedClient red_client = null;
    private static BlueClient blue_client = null;
    private static RedServer red_server = null;
    private static BlueServer blue_server = null;

    public static void main(String[] args) {
        final Client myClient = new Client(arg1, arg2);
        red_client = myClient.getRedClient();
        blue_client = myClient.getBlueClient();

        final Server myServer = new Server(arg3, arg4);
        red_server = myServer.getRedServer();
        blue_server = myServer.getBlueServer();
    }

    public static RedClient getRedClient() {
        return red_Client;
    }

    public static BlueClient getBlueClient() {
        return blue_client;
    }

    public static RedServer getRedServer() {
        return red_server;
    }

    public static BlueServer getBlueServer() {
        return blue_server;
    }

}

I would later make use of the following like so:

public class Client {
    public void SomeMethod {
        MyApp.getBlueServer.doSomething(myObject);
    }
}

I'm just unsure if this is a proper way of passing the instance to the other class as Client and Server both communicate with MyApp. (Please ignore the class names as they have nothing to do with functionality of the application, I just used them as names because it was the first thing I could think of).

If you need any clarification let me know, as I'm open to learning and criticisms. If this is the wrong way, can you please explain why it is wrong and then explain the proper way.

EDIT

Further clarification:

No other classes have access to each other.

Upvotes: 0

Views: 1086

Answers (4)

Tom
Tom

Reputation: 4180

you could work with singletons. If you have a class and you can be sure that there's always maximum one instance of, then that instance is a singleton. I'll give you a small example:

public class MyClass {
    private static MyClass instance;
    public static MyClass getInstance() {
        if(instance == null)
            instance = new MyClass();
        return instance;
    }
    private MyClass() { }
}

with this, from anywhere in your code, you can call MyClass.getInstance() and you'll get a singleton; think about it; It can be very practical for the situation you talk about.

Upvotes: 1

Ziggurat
Ziggurat

Reputation: 257

I am not sure what you are asking for, but I think you need to read a little about Object Oriented Programming first.

For the case you just presented (I don't like it but it's better I think) you may have the Client constructor to recieve the server as a parameter:

Client client1 = new CLient(server1)

so you will be able to access the server1 object from client methods.

Upvotes: 1

jahroy
jahroy

Reputation: 22692

I don't see you passing/setting anything.

There's no reason you can't pass instances of clients to servers (and vice versa):

Client myClient = new Client(args);
Server myServer = new Server(args);

BlueClient = myClient.getBlueClient();
RedCLient = myClient.getRedClient();

BlueServer = myServer.getBlueServer();
RedServer = myServer.getRedServer();

myClient.addServer(blueServer);
myClient.addServer(redServer);

myServer.addClient(blueClient);
myServer.addClient(redClient);

Upvotes: 2

Related Questions