0

Why does a layout parameter required to be declared final?

As follows:

@Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Login form");

        BorderPane borderLayout = new BorderPane();
        borderLayout.setCenter(login(borderLayout));


        Scene scene = new Scene(borderLayout, 300, 275);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

private GridPane login(final BorderPane rootLayout){  //<<--
     // CODE
     return grid;
}

EDIT: final BorderLayout shud've been BorderPane.

Taerus
  • 475
  • 6
  • 20
  • Check out the answers to the same question here http://stackoverflow.com/questions/4930114/why-declare-a-function-argument-to-be-final – takendarkk Jan 18 '14 at 16:01
  • Why would you use `BorderLayout` at all? Also, do you realize you are passing `BorderPane` instance in login method instead of `BorderLayout` instance – Branislav Lazic Jan 18 '14 at 16:02
  • @ csmckelvey Thanks! Certainly makes sense, but what i'm doing inside the login method is actually doing rootLayout.setCenter(otherMethod(rootLayout)) or is this not what you mean by changing it? Cos it certainly does not restrict me from doing that. – Taerus Jan 18 '14 at 16:05
  • @ brano I wrote that part really fast for this question, it should ofc be BorderPane. – Taerus Jan 18 '14 at 16:06
  • Then you got your answer. – Branislav Lazic Jan 18 '14 at 16:07
  • Try doing something like `rootLayout = new BorderPane();` – takendarkk Jan 18 '14 at 16:07

1 Answers1

3

Taken from user templatetypedef:

There are two main reasons you might want to mark an argument final. First, if you're planning on using the argument in an anonymous inner class, then you must mark it final so that it can be referenced in that class. This is actually a pretty common use case for marking arguments final.

The other common reason to mark arguments final is to prevent yourself from accidentally overwriting them. If you really don't want to change the arguments, then perhaps you should mark them final so that if you actually do, you'll get the error at compile-time rather than finding out at runtime that your code has a bug.

takendarkk
  • 3,347
  • 8
  • 25
  • 37