title: How to quit a function if variables changes in java ? (restart server)
body: <p>Using Java, I am looking for a way to quit a function if one variable changes. Idea ? </p>
  
  <p>Something like this : </p>
  
  <pre><code>public class MyServer{
  
      private int port = 6789;
  
  
      public static void main(String args[]){
           startServer(port);    // My server is listenning on port 6789 first
      }
  
      public void handlePortChange(){
           // if 'port' changes, restart server
      }
  
      public void setPort(int p) { port = p; }
  }
  
  
  public Class MyServerUI{
  
      JTextField textField;
      MyServer server;
  
      // Constructor...
      MyServerUI(MyServer s){
           server = s;
      }
  
      // Method that
      public void openUI(){
  
           // My window
           JFrame  f  = new JFrame("My Server");
           f.setVisible(true);
  
           // a JtextField
           t = new JTextField("New port");
           f.add(t);
  
           // a button
           JButton b = new JButton("Change port");
           b.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e){
                     // Changing the port
                     server.setPort(Integer.parseInt(t.getText()));
                     // method that restart server if port changes
                     server.handlePortChange();   
                }
           });
           f.add(b);
      }
  
  }
  </code></pre>