title: How to share the same variable between class instances?
body: <p>(UPDATE)So I am running a server and I want a certain number of clients to connect, so in the client class each time a client connects I increment a counter, but it does not seem to be working. I am trying to do it on the server side but its still not working </p>
  
  <pre><code> while(true){
          try{
              Socket clientSocket = serverSocket.accept();
              MortgageRunnable m = new MortgageRunnable(clientSocket);
              System.out.println("New player has connected!");
              if(!(connectedClients == maxPlayers)){
              new Thread(m).start();
              connectedClients++;}
              else{m.displayMessage("Max players connected");}
  </code></pre>
  
  <p>MortgageRunnable is basically the thread so all logic is there, and this is the function displayMessage():</p>
  
  <pre><code>public void displayMessage(String message) {
      try {
          PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
          out.println(message);
  }catch(IOException e){
          e.getMessage();
      }
  </code></pre>