Java final variable

Source: Internet
Author: User

Final variable definition: Once a variable is initialized, it cannot point to another object. The storage address pointed to cannot be modified, but the object to which it is pointing can be modified.

First, the final variable is initialized:

Many articles say this: its initialization can be in two places, one is its definition, and the other is in the constructor, the two can only be selected.
Nonsense!
The final variable can be initialized in any place that can be started, but only once. Once initialized, it cannot be assigned again.
Value (re-pointing to other objects), must be explicitly initialized as a member variable, and as a temporary variable you can define not initialize (and of course not)
Even as a member variable in a class, it can also be initialized in the initialization block, so "its initialization can be in two places, one is its definition,
Second, in the constructor, the two can only choose one "is wrong.


As a member variable, the final field can be designed with immutable classes, which is a necessary condition for the invariant class, but not a sufficient condition. At a minimum, you can guarantee that the field is not
Will change in such a way as setxxx (). But there is no guarantee that the field itself is not modified (unless the field itself is a constant class);

For the final variable of the method parameter:
For a variable of a method parameter defined as final,90%, the article says "When you do not need to change an object variable as a parameter in the method, explicitly make the
Declaring with final will prevent you from inadvertently modifying variables that are outside of the calling method. "
Nonsense!

I don't know if this change is about re-assigning or modifying the object itself, but in either case, the above statement is wrong.
If you are saying re-assignment, then:
public static void Test (int[] x) {
x = new int[]{1,2,3};
}

Int[] out = new int[]{4,5,6};
Test (out);
System.out.println (Out[0]);
System.out.println (out[1]);
System.out.println (out[2]);
Call test (out); it does not affect the outside variable out anyway. It doesn't make any sense to add to your final. Final will only force the inside of the method
Declare a variable name, that is, x = new int[]{1,2,3}, and change to int y = new int[]{1,2,3}; The other does not have any practical significance.
If you are modifying the object itself:
public static void Test (final int[] x) {
X[0] = 100;
}
Int[] out = new int[]{4,5,6};
Test (out);
System.out.println (Out[0]);
Can't you modify it with the final decoration? So it's nonsense to say that final in the method parameter is not affecting variables outside of the calling method.

So why do we have to add final to the parameters? In fact, the method parameter plus final and method internal variables plus final function is the same, that is, in order to put them
Passed to the inner class callback method:

Abstract class absclass{
public abstract void M ();
}

Now let's see if I want to implement an anonymous call to Absclass in a method. Should:
public static void Test (String s) {
or string s = "";
Absclass C = new Absclass () {
public void M () {
int x = S.hashcode ();

SYSTEM.OUT.PRINTLN (x);

}
};
Other code.
}
Note here, in general, that the callback method is basically called in other threads. If we're on top of the
Absclass C = new Absclass () {
public void M () {
int x = S.hashcode ();

SYSTEM.OUT.PRINTLN (x);

}
};
Call C.M () directly behind it; it should be meaningless. But it doesn't matter, it's important that as long as it's possible to call in another thread, we have to
Save the reference handle for S.

Let's take a look at how GC works, and each process in the JVM will have multiple roots, each static variable, method parameter, local variable, and of course this refers to a reference type.
The underlying type is not a root, and the root is actually a storage address.

When the GC works, it iterates through the objects it references and marks them from the root, so recursively to the nearest, all the roots are traversed, and no object description is marked.
There is a reference to the object that can be recycled (some objects have the finalized method, although there is no reference, but there is a dedicated queue in the JVM that references it
Until the finalized method is executed, it is not removed from the queue as a truly unreferenced object and can be recycled, which is irrelevant to the discussion in this topic, including
The division of the Generations is explained later). This looks good.

But in the callback method of the inner class, s can neither be a static variable nor a temporary variable in a method, nor a method parameter, it cannot be a root, in an inner class
There is no variable referencing it, its root is in that method outside the inner class, and if the outside variable points to another object, then the object loses its reference.
May be recycled, and since most of the internal class callback methods are executed in other threads, it may also continue to be accessed after recycling. What will this result be?

Using the final modifier will not only keep the object from changing, but the compiler will also continue to maintain the object's life cycle in the callback method. So this is final.
The fundamental meaning of the variable and final parameter.

Java final variable

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.