Use of final local variables in java [duplicate]

2019-01-23 01:34发布

I was wondering is there any usability of using final local variables. Variables are not overridden anyway when inheritance comes into picture. For example a simple code as below

public static void main(String args[]) {
    final String data = "Hello World!";
    System.out.println(data);
}

The example is quite simple one and may not be a relevant code but the question is more generic.I have seen a lot of codes(all incorporated in main function which have final local variables) Is there any usability of declaring local variables as final other than that they cannot be edited in the same function itself?

8条回答
SAY GOODBYE
2楼-- · 2019-01-23 02:01

Use of final local variables in java

final fields, parameters, and local variables are read-only(means the object's identity, not its state).

查看更多
老娘就宠你
3楼-- · 2019-01-23 02:01

You might use final keyword for readability and ensuring it is not modified or in anonymous classes like here:

    final String s = "hi";
    new Thread(new Runnable() {
        @Override
        public void run() {
            // can use s variable here
        }
    }).start();
查看更多
女痞
4楼-- · 2019-01-23 02:06

final local variables may be accessed from anonymous inner subclasses, whereas non final local variables may not.

查看更多
ら.Afraid
5楼-- · 2019-01-23 02:07

Firstly, the part about variables being "overridden" - final has two very different meanings. For classes and methods, it's about inheritance; for variables it's about being read-only.

There's one important "feature" of final local variables: they can be used in local (typically anonymous) inner classes. Non-final local variables can't be. That's the primary use of final for local variables, in my experience.

public void foo() {
    final String x = "hello";
    String y = "there";

    Runnable runnable = new Runnable() {
        @Override public void run() {
            System.out.println(x); // This is valid
            System.out.println(y); // This is not
        }
    };
    runnable.run();
}

Note that as a matter of style, some people like to use final even when they're not capturing the variable in a local inner class. I'd certainly be comfortable with final being the default, but a different modifier for "non-final", but I find that adding the modifier explicitly everywhere is too distracting.

查看更多
6楼-- · 2019-01-23 02:09

Yes, the usability is :- local final variable are accessible by the method inner class. Since local variables live on the stack and exist only for lifetime of the method but Inner class object may live longer so inner class can't access any non final local variable. But once the variable is final then the inner class sure that the value won't change so it keep a private copy of the final local variable for use.

查看更多
甜甜的少女心
7楼-- · 2019-01-23 02:09

It tells the other programmers that whoever wrote it knew that the value of data shouldn't change once assigned. It's a good habit in my opinion especially declaring the parameter variables as final.

查看更多
登录 后发表回答