title: Setting value from one class to another in Java
body: <p>I have got three Java classes:</p>
  
  <ul>
  <li>The First has a setter and a getter</li>
  <li>The Second has a JComboBox</li>
  <li>The third has a MySQL query and generating list.</li>
  </ul>
  
  <p>I used the setter to set the value and I set the value from the combo box class. </p>
  
  <p>Now, I would like to get this value in another class.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public class Settings {
  
      private static String RootName;
  
      public static void setRootName(String rootName){
       RootName = rootName;
      }
  
      public static String getRootName(){
       return RootName;
      }
  }
  </code></pre>
  
  <p>combobox.java</p>
  
  <pre><code>public class ComboBoxDemo extends JPanel
                            implements ActionListener {
      String connectionURL = "jdbc:mysql://localhost:3306/Trainpis";
      JLabel picture;
      public static String i="hello";
  public String rootname;
      public ComboBoxDemo()  {
  
   JComboBox combo=new JComboBox();
  combo.addActionListener(this);
  
  
  
  JFrame f=new JFrame();
  JPanel p=new JPanel();
  try{
  Class.forName("com.mysql.jdbc.Driver");
  java.sql.Connection conn = DriverManager.getConnection(connectionURL, "root", "");
  Statement st=conn.createStatement();
  ResultSet rs=st.executeQuery("Select route from route");
  while(rs.next()){
  combo.addItem(rs.getString("route"));
  //System.out.println(rs.getString("route"));
  }
  }
  catch(Exception e){}
  p.add(combo);
  f.add(p);
    f.setExtendedState(JFrame.MAXIMIZED_BOTH); 
    f.setUndecorated(true);
  f.setVisible(true);
  
   }
  
      /** Listens to the combo box. */
      public void actionPerformed(ActionEvent e) {
       JComboBox cb = (JComboBox)e.getSource();
          String selectedRoute = (String)cb.getSelectedItem();
         // System.out.println(rootname);
          String root1="Huda City Center - Noida City Center";
         if(selectedRoute.equalsIgnoreCase(root1))
         {
             System.out.println("hello");
       //new Test();
             //Settings mysettings = new Settings();
     Settings.setRootName(selectedRoute);
  
      RootSelection1 r1 = new RootSelection1();
          r1.print();
         }
         else{
         System.out.println("bye");
         } 
      }
  
     public static void main(String[] args) {
          //Schedule a job for the event-dispatching thread:
          //creating and showing this application's GUI.
          javax.swing.SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  new ComboBoxDemo();
              }
          });
      }
  }
  </code></pre>
  
  <p>Now I want to use the selected value of combo box:</p>
  
  <pre><code>String rootSelection = Settings.getRootName();
  String selectStoredProc = "SELECT sino,stationname,distance from station where route ='"+rootSelection+"'";
  String [] root;
  
  try{
      Class.forName("com.mysql.jdbc.Driver");
      Connection conn = DriverManager.getConnection(connectionURL, "root", "");
  
      PreparedStatement ps = conn.prepareStatement(selectStoredProc);
      ResultSet rs=ps.executeQuery();
      while(rs.next()){
          String s1=rs.getString("stationname"); 
          nameList.add(s1);
          root = nameList.toArray(new String[nameList.size()]);
      }
  }
  catch(Exception e){}
  </code></pre>
  
  <p>I want to do all of this on combo box's selected item.</p>
  
  <p>How can I achieve this?</p>