Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Jbutton in thread prints 0 when fetching constructor value

Dinud123Mar 21 2013
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 ??

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Apr 18 2013
Added on Mar 21 2013
0 comments
911 views