There are two classes, in the main window there is a button and a label, the button launches the method, which in turn using a static method of another class opens a new window to String txt, this txt should be changed in the second class method, using the button, but since in lambda, you can wield only final or effective-final variables, I created a temporary String which I then try to assign to the transmitted value, but it does not work.

public class JavaFXApplication1 extends Application { String post = "no changes"; Label lbl = new Label("no changes"); @Override public void start(Stage primaryStage) { Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction((ActionEvent event) -> { SomeChanger.changeSome(post); lbl.setText(post); }); VBox root = new VBox(); root.getChildren().addAll(btn,lbl); Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } } public class SomeChanger { static String temporarySt = "no changes"; public static void changeSome(String st){ //st=temporarySt; Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction((ActionEvent event) -> { temporarySt = "changed st"; System.out.println("bla"); }); VBox root = new VBox(); root.getChildren().add(btn); Scene scene = new Scene(root, 300, 250); Stage primaryStage = new Stage(); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.showAndWait(); st=temporarySt; } } 

There are only a few mandatory conditions that can not be changed:

  1. Non-static variable is passed to the static method of another class.
  2. The method of this class should open a dialog box.
  3. The same method should via the button replace the variable being transferred so that it changes in the first grade.
  • one
    The kmk problem is not connected with lambdas, but with an attempt to change the value of a method parameter (in java, all parameters are passed by value). As far as I can see, changeSome will end only after the window is closed, so you can simply make a return temporarySt and in the calling code lbl.setText( SomeChanger.changeSome(post) ); . Instead of a static field to get a result from lambda, it is better to use a local array, although the difference in this case is not great. - zRrr pm

3 answers 3

The most clumsy solution is to use an array of one element.

final static String[] temporarySt = {"no changes"};

and then

 temporarySt[0] = "changed st"; 
  • Does not work, the label on the first window has not changed - arachnoden
  • If you need to change the label, pass the Label object to changeSome and call the setText method. public static void changeSome(final Label lbl) - Nikolay Romanov

You might like StringProperty

    The easiest way to declare a static variable in the class and to change its value, in this case, Java will not swear.