Forum Stats

  • 3,837,134 Users
  • 2,262,229 Discussions
  • 7,900,211 Comments

Discussions

Jbutton in thread prints 0 when fetching constructor value

hello, Two classes ..

public class constg {
public static void main(String[] args)
{
threadconst tc=new threadconst();
tc.start();
tc=new threadconst(5);
tc.stop();
}
}

------------------------------------------------------------------------------------
public class threadconst extends Thread {
JFrame frame=new JFrame();
JButton ack=new JButton("ACKNOWLEDGE ME !!");
int p;

threadconst(int i)
{
this.p=i;
System.out.println(p); //prints 5
}

public threadconst() {
// TODO Auto-generated constructor stub
}

public void run()
{
ack.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
System.out.println("button "+p); ///prints button 0
}

});
ack.setBounds(0,0,300,300);
frame.add(ack);
frame.setVisible(true);
}
}

y does the button variable print value 0 instead of 5 ??
How to overcome this problem. ? plzz help ??
Tagged:
This discussion has been closed.