The code you have in your question is an initializer. As a result, Example.getExampleString()
is called once when the variable is first defined in memory, and after that, example
will contain the value the function returned that one time it was called.
So if you run your program twice, the variable could have a different value for each run, but once the variable is set, it can't be changed while the program is running.
As an example, try this:
import java.util.Random;
public class VarTest {
private static final Random rng = new Random();
private static final String example = Integer.toString(rng.nextInt());
public static void main(String[] args) {
System.out.println(example);
System.out.println(rng.nextInt());
System.out.println(rng.nextInt());
System.out.println(example);
}
}
If you run this program, you'll see that nextInt
returns different values with each call, but example
remains constant within the program. If you run it a second time, example
can be different than the last time you ran it, but example
still doesn't change during runtime. (You may need to wait a few seconds or minutes between runs to see different values of example
because of how Random
works.)