-1

Can you please help me with the answer, please? I've read a lot, but can understand, how to organize the code. I want to create my own method to work with the Network and call it time to time in a program. But this compilation error makes me mad:

variable might be not have been initialized

I do understand why, but I can`t see solution(( My vision is to open Socket and related streams only one time and close it when needed.

public static void socket_r (String action, String ip_addr, String to_write, int port)  throws IOException {    

String s;

switch (action) {

            case "Create":      Socket socket = new Socket (ip_addr, port);
                                OutputStream out_from_socket = socket.getOutputStream();
                                PrintWriter writer_socket = new PrintWriter(out_from_socket, true);

                                InputStream input_socket = socket.getInputStream();
                                BufferedReader reader_socket = new BufferedReader(new InputStreamReader(input_socket));

                                break;                                  

            case "Write":       writer_socket.println(to_write);            
                                writer_socket.println();                

                                break;

            case "Read":        while ((s = reader_socket.readLine()) != null) System.out.println(s);

                                break;

            case "Close":       writer_socket.println(to_write);    
                                writer_socket.println(); 
                                writer_socket.close();
                                reader_socket.close();                                      
                                break;
                }               

}
user207421
  • 305,947
  • 44
  • 307
  • 483
Alex
  • 1
  • 1
  • 1
    If you examine the code path your function will take if called with an `action` argument *that isn't* `create`, you might see why you're getting that error. – jedwards Jun 20 '18 at 22:32
  • perhaps you should look up the singleton design pattern for what you are trying to accomplish, it might be what you are looking for – RAZ_Muh_Taz Jun 20 '18 at 22:43
  • 1
    The solution is not to write code like this. Write four separate methods. – user207421 Jun 20 '18 at 22:53

1 Answers1

1

Lets start with why the code won't compile ...

The variables reader_socket and writer_socket are local variables. That means:

  1. they must be definitely assigned before you can use them, and
  2. they disappear ... along with their respective values ... when your socket_r method returns to the caller.

In your code, there are lots of places where reader_socket and writer_socket are used without being definitely assigned. For example:

    writer_socket.println(to_write);

A call to socket_r can reach that statement without passing through any statement that assigns a value to writer_socket. (Yes. Look at it.)

But here's the problem. Your design requires that a socket_r("write", ...) call uses state that only gets initialized in a socket_r("create", ...) call. And then it gets thrown away.

Basically, you cannot implement that design using local variables for the state.

So what is the solution?

In order of "goodness"

  1. Make the variables static variables in the enclosing class. (This is a bad solution, but it involves the least change to your existing code. Why it is bad is ... well ... read some more about OO and why static should be rarely used.)
  2. Make the method non-static method and make the variables instance variables.
  3. Solution 2 AND replace socket_r with a separate method for each "action".
  4. Solution 3 AND put in some extra logic to enforce a proper "life-cycle" for the socket; i.e. create before reads and writes, no reads or writes after close, etcetera.

Basically, you need to rethink your design to do things in an OO way, and it will get a whole lot easier.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Well yes, you absolutely correct, I have to rethink design and yes, I feel that my code is more procedure/functions like in a past insted of OO. But I have some problem with it, as then you have to design main code around operations with Socket. Well, thanks for advice in general, I will thnik how to do refactoring. – Alex Jun 21 '18 at 08:45