public static void mainString args final Student1 s = new Student1; s = new Student1; sage = 18; class Student1 int age; public Student1 为什么sage = 18;可以赋
在给变量s赋值时,s被声明为final,意味着它的引用不能被修改。因此,s = new Student1(); 这行代码会导致编译错误,因为它试图修改final变量s的引用。只有在声明时,final变量可以被赋值一次,之后不能再次赋值。但是,可以修改final变量所指向的对象的属性,例如s.age = 18; 这行代码是可以执行的,因为它只是修改了s所指向的对象的属性值,而不是修改s的引用。
原文地址: http://www.cveoy.top/t/topic/ii28 著作权归作者所有。请勿转载和采集!