title: access private variable from other class in java
body: <p>i hope that i mean my words.
  i have a class like this:</p>
  
  <pre><code>public class MainClass extends JFrame{
      private JLabel mainlabel;
      private SampleClass sample=new SampleCalss();
  
      public void intital(){
          mainlabel=new JLabel("Main");
          sample.setMethod(getLabel());
          //
          //some code
          //
          add(mainlabel); 
      }
  
      public static void main(){
          intital();
      }
  
      public JLabel getLabel(){
          return mainlabel;
      }
  }
  </code></pre>
  
  <p>and other class like this:</p>
  
  <pre><code>public class SampleClass extends JFrame{
      private JButton button=new JButton("Change");
      private JLabel sLabel;
  
      public SampleClass(){
          //somecode
          //
          button.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  sLabel.setText("Sample text set");
              }
          });
          add(jButton);
      }
  
      public void setMethod(JLabbel l){
          sLabel=l;
      }
  }
  </code></pre>
  
  <p>is this a true way to access <code>mainlabel</code> and change its value from other class(in this sample code in class <code>SampleClass</code>) is there a better or right solution?
  note that <code>MainClass</code> is class that has <code>main</code> method.</p>