/*
Interview questions: Final modification of local variables
Base type: The value of the base type cannot be changed.
Reference type: The address value of a reference type cannot be changed, but the value of the object's heap memory can be changed.
*/
Class Student {
int age = 10;
}
Class Finaltest {
public static void Main (string[] args) {
Local variables are basic data types
int x = 10;
x = 100;
SYSTEM.OUT.PRINTLN (x);
Final int y = 10;
Unable to allocate value for final variable y
y = 100;
System.out.println (y);
System.out.println ("--------------");
Local variables are reference data types
Student s = new Student ();
System.out.println (S.age);
S.age = 100;
System.out.println (S.age);
System.out.println ("--------------");
Final Student ss = new Student ();
System.out.println (Ss.age);
Ss.age = 100;
System.out.println (Ss.age);
Reassigning memory Space
Unable to assign value to the final variable SS
SS = new Student ();
}
}
————————————
The initialization time for the
/*
Final modifier variable
A: A variable that is final decorated can only be assigned one time.
B: Before the construction method completes. (non-static constants)
*/
Class Demo {
//int num = n
Final int num2 = 20;
int num;
final int num2;
{
//num2 = ten
}
Public Demo () {
num =;
Unable to allocate value for final variable num2
num2 =;
}
}
class FinalTest2 {
public static void Main (string[] args) {
Demo d = new demo ();
System.out.println (D.num);
System.out.println (D.NUM2);
}
}