Final reserved word question.

Asked 2 years ago, Updated 2 years ago, 25 views

void method1()
{
    final int a = 10;
    TextView tv = (TextView)findViewByID(R.id.btn);
    tv.setOnClickListener(new OnClickListener() {
        @Override
        void onClick(View v)
        {
                a++;
        }
   });
}

Here, if variable a is not specified as final, it cannot be used equally within the onClick method due to the life cycle difference. Then does JVM convert the final here to static? If it is converted to static, should the variable a be identical to the one declared static, such as alive on memory, even if method 1 ends? Thank you.

java android

2022-09-22 15:43

2 Answers

In conclusion, the value of a exists in memory even after the method1() function is called. Quoting Wikipedia's content operates by capturing and storing the value of the variable declared final as a field of the internal class at runtime.That's what it says. The reason why you can access the value of a even after method1() is over is because you should refer to this part.

When an anonymous inner class is defined within the body of a method, all variables declared final in the scope of that method are accessible from within the inner class. For scalar values, once it has been assigned, the value of the final variable cannot change. For object values, the reference cannot change. This allows the Java compiler to "capture" the value of the variable at run-time and store a copy as a field in the inner class. Once the outer method has terminated and its stack frame has been removed, the original variable is gone but the inner class's private copy persists in the class's own memory.


2022-09-22 15:43

In Java, final and static are not complementary relationships.

To understand static, you need to distinguish between classes and instances by default.

The relationship between a class and an instance can also be likened to a drawing (class) and a figure (instance) created by the drawing. At this time, static in Java means a member for the class itself, not a member of the instance.

A class member variable declared staitc has a life cycle from the time the class is loaded into memory to the time the class is unloaded on memory.(However, it is difficult to find a class unloaded in most cases, unless you are specifically trying to unload it.)

Java's final keyword means constant. It's the difference between a variable and a constant, and there's a general constant, for example, 1, 2, 3, but in reality, we also use letters to express the constant, like the gravitational acceleration constant g. The keyword that looks like a variable in the program, but makes it recognized as a constant is final.

For inta = 10 in the example above, variable a. The way to convert this a into a constant is to use the final keyword. Now that a is no longer a variable, it no longer has a life cycle of a variable. When we write a program, we can understand it like writing numbers 1, 2, 3, and so on everywhere.

Final means constant

Comment


2022-09-22 15:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.