title: How to use existing Objects in an overwritten run() Method?
body: <p>I'm a beginner in programming and do strugle a bit.
  So I'm building a TCP peer to peer Chat and that requires me to divide the tasks -&gt; Threads.
  So I want to built a Thread for the &quot;writing&quot; part of that connection (Scanner, DataOutputStream etc.) In order to do that i implemented Runnable and that forces me to write my Thread in the overwritten run() method.
  Now I have a bit of a problem, because in Order to send my messages out to the &quot;other end (another client) I need the &quot;socket.getOutputStream&quot; but I cant use it in the run() method and i dont know how to fix this problem, sitting already a week on this problem. Any ideas ?</p>
  <pre><code>public class ClientHorcher implements Runnable {
  
      public static void main(String[] args) {
          
          try {
              ServerSocket serverSocket = new ServerSocket(11111);
              System.out.println(&quot;Waiting For Connection:-&quot;);
              Socket socket = serverSocket.accept();
              Scanner scanner = new Scanner(System.in);
              DataInputStream datenRein = new DataInputStream(socket.getInputStream());
              DataOutputStream datenRaus = new DataOutputStream(socket.getOutputStream());
              String nickname;
              System.out.print(&quot;Gib einen Nickname ein: &quot;);
              nickname = scanner.nextLine();
              while (true) {
                  String vonMsg = datenRein.readUTF(in);
                  System.out.println(&quot;Client:-&quot; + vonMsg);
                  if (vonMsg.equals(&quot;exit&quot;)) {
                      System.out.println(&quot;Beenden!!!&quot;);
                      datenRein.close();
                      datenRaus.close();
                      scanner.close();
                      socket.close();
                      serverSocket.close();
                      System.exit(0);
                  }
  
                  System.out.print(nickname + &quot;:-&quot;);
                  String zuMsg = scanner.nextLine();
                  datenRaus.writeUTF(zuMsg);
                  if (zuMsg.equals(&quot;exit&quot;)) {
                      System.out.println(&quot;Quiting!!!&quot;);
                      datenRein.close();
                      datenRaus.close();
                      scanner.close();
                      socket.close();
                      serverSocket.close();
                      System.exit(0);
                  }
              }
          } catch (IOException e) {
          
              e.printStackTrace();
          }
  
      }
  
      @Override
      public void run() {
         
          
      }
  
  }`
  `public class ClientVerbinder implements Runnable  {
      
  
  
      public static void main(String[] args) {
  
          try {
              Socket socket = new Socket(&quot;Localhost&quot;, 11111);
              System.out.println(&quot;Connected&quot;);
              Scanner scanner = new Scanner(System.in);
              DataInputStream datenRein = new DataInputStream(socket.getInputStream());
              DataOutputStream datenRaus = new DataOutputStream(socket.getOutputStream());
              String nickname;
              System.out.print(&quot;Gib einen Nickname ein: &quot;);
              nickname = scanner.nextLine();
  
              while (true) {
                  System.out.print(nickname+&quot;:-&quot;);
                  String zuMsg = scanner.nextLine();
                  datenRaus.writeUTF(zuMsg);
                  if (zuMsg.equals(&quot;exit&quot;)) {
                      System.out.println(&quot;Beenden!!!&quot;);
                      datenRein.close();
                      datenRaus.close();
                      scanner.close();
                      socket.close();
                      System.exit(0);
                  }
                  String vonMsg = datenRein.readUTF();
                  System.out.println(&quot;CLient&quot;+&quot;:-&quot; + vonMsg);
                  if (vonMsg.equals(&quot;exit&quot;)) {
                      System.out.println(&quot;Quiting!!!&quot;);
                      datenRein.close();
                      datenRaus.close();
                      scanner.close();
                      socket.close();
                      System.exit(0);
                  }
              }
          } catch (UnknownHostException e) {
              
              e.printStackTrace();
          } catch (IOException e) {
              
              e.printStackTrace();
          }
  
      }
      @Override
      public void run() {
         
          
      
      }
  
  }
  </code></pre>