What exactly a constructor does and why this code is not working in constructor?
<p>I read about the constructer that a constructor is something that is used to set up the components of the container. So i wrote the following program which works well but only problem is that the label is not at the specified location i.e. 125,300. When I wrote the line label.setLocation(125,300) in the public run() then the code works well. Why it is not working when I write the line in the constructor? Basically it should work as the constructor is used to set up the components of the JFrame. </p>

<p>If not then What exactly is a constructor?</p>

<p>The Code:</p>

<pre><code>public class RealGame extends JFrame implements Runnable, KeyListener{

JLabel label = new JLabel("I am a JLabel");
RealGame(){
    setVisible(true);
    requestFocus();
    setContentPane(new JPanel());
    getContentPane().setSize(640, 480);
    setSize(640, 480);
    label.setSize(50,50);
    label.setOpaque(true);
    label.setVisible(true);
    label.setFont(new Font("Arial", Font.PLAIN, 18));        
    getContentPane().add(label);

}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new RealGame());
}


@Override
public void run() {
    label.setLocation(125,300); //THIS IS IMPORTANT
    JOptionPane.showMessageDialog(null, "Hello");
    System.out.println("Done");
    System.exit(0);
}


@Override
public void keyTyped(KeyEvent e) {

}


@Override
public void keyPressed(KeyEvent e) {

}


@Override
public void keyReleased(KeyEvent e) {

}

}
</code></pre>

<p>And also is the structure of the program correct? i.e. in many programs I saw that people use</p>

<pre><code> SwingUtilities.invokeLater(new Runnable(){

   public run(){

   }

 });
</code></pre>

<p>But instead I instantiated the main class. Will it give any problems in future?</p>