0

Below I am trying to assign the value of String temp to String test so that it can be worked on outside the actionlistener class. I get an error that the varible must be final or effectively final. Is there a way to get the value of temp outside the class so that it can be worked on?

String test;
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
  temp = UserScreen.textField.getText();
  UserScreen.messageAppend(temp); 
  test = temp;
 }
});

2 Answers2

0

An actionListener is called only when an action occurs, so just after you declare it in your function, the String test will be null.

You could declare a string test as a member variable of your class and then reuse it later.

Theo
  • 157
  • 12
0

In order of that to work you should declare the String test; as a class variable aND not there.

Another option would be declaring it as final, but that makes no sense in your code because once declared yout wouldn't be able to change it's value...

So option 1 is the way to be in your case

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97