How to set a variable to be final or effectively final

3102 views java
8

Im currently working on my project for ap computer sci and I wanted to see if I could innovate by including a javafx scene in my code but I am having a issue with some of my code.

            });
            applicationStage.setScene(scene);    // Set window's scene
            applicationStage.setTitle(""); // Set window's title
            applicationStage.show();             // Display window

         }  


         public void middle(Stage secondApplicationStage) {
         }
       });
      }
        public static void main(String[] args) {
            launch(args); // Launch application
         }
   }

answered question

Interesting. Whats your problem?

What actually is the question here?

There is not enough code to tell where (effectively) final variables are needed and why those variables are not effectively final. You need to post more information.

We cannot see what is your problem. Please provide a Minimal, Complete, and Verifiable example so that you can get the help a bit faster.

1 Answer

6

To set a variable to be final, you just use the final keyword. I'm assuming you're asking this because of lambdas, because that's usually where final or effectively final variables come into play, from my experience.

Note that final does not mean immutable. All it means is that you can't change the value of variable itself, whether that be a primitive or a reference. It doesn't mean you can't call methods that change the object on the heap that the reference is pointing to.

final int myNumber = 10;

Runnable myRunnable = () -> {
  System.out.println(myNumber);
};

Example of what I mean by final not being immutable:

final Set<Integer> mySet = new HashSet<>();
mySet.add(1);

posted this

Have an answer?

JD

Please login first before posting an answer.