final

Does final imply override?

风格不统一 提交于 2020-01-30 14:20:53
问题 As I understand it, the override keyword states that a given declaration implements a base virtual method, and the compilation should fail if there is no matching base method found. My understanding of the final keyword is that it tells the compiler that no class shall override this virtual function. So is override final redundant? It seems to compile fine. What information does override final convey that final does not? What is the use case for such a combination? 回答1: final does not require

Java中的final关键字

。_饼干妹妹 提交于 2020-01-30 13:40:18
1.final修饰类 当类被修饰成final的时候,表示该类不能够被继承,其子类会出现编译错误 2.final修饰方法 父类的方法被修饰成final,那么该方法在子类中中,不能够被重写 3.final修饰基本类型变量 final修饰基本类型变量,表示该变量只有一次赋值机会 4.final修饰引用 引用被修饰成final,表示该引用只有 1次指向对象的机会 public class Hero extends Object { String name; //姓名 float hp; //血量 float armor; //护甲 int moveSpeed; //移动速度 public static void main(String[] args) { final Hero h; h =new Hero(); h =new Hero();              //此处会报错 h.hp = 5; } } 来源: https://www.cnblogs.com/z-cg/p/12242598.html

java之final关键字

大憨熊 提交于 2020-01-23 17:52:48
final可已修饰类 方法 变量; 修饰类 不可以被继承 修饰方法 不可以被子类重写 修饰变量 修饰基本类型变量时: 变量在初始化后值不能发生变化; 修饰引用类型变量时: 变量在初始化后不能再指向其它变量,地址值不能改变;对象的属性是可以修改的; public class Demo { private static final int a = 1 ; private static final Student student = new Student ( "小明" , null , null , 0 , new Date ( ) ) ; public static void main ( String [ ] args ) { a = 2 ; //编译报错 基本类型初始化后不能被修改 student . setAge ( 12 ) ; //编译通过 引用类型修改对象属性值可以 student = new Student ( "小明" , null , null , 0 , new Date ( ) ) ; //编译报错 引用类型地址值改变 } } 来源: CSDN 作者: 藏獒的故事 链接: https://blog.csdn.net/weixin_40292351/article/details/104076107

why I can`t use method get(java.lang.reflect.Field#get) before changing field`s modifiers

让人想犯罪 __ 提交于 2020-01-21 12:07:09
问题 java code as follow. import java.lang.reflect.Field; import java.lang.reflect.Modifier; public class Test { public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { C c = new C(); Field field = c.getClass().getDeclaredField("NAME"); field.setAccessible(true); System.out.println(field.get(c));//Cause program exception on line 15 while using method get(java.lang.reflect.Field#get). Field modifiers = field.getClass().getDeclaredField("modifiers"); modifiers

How can I initialize interdependent final references?

无人久伴 提交于 2020-01-15 07:08:34
问题 I have a class and a factory function that creates new anonymous class objects extending that class. However, the anonymous class objects all have a method in which there are references to other objects. In my full program, I need this to create and combine parsers, but I've stripped down the code here. class Base{ public Base run(){ return null; } static Base factory(final Base n){ return new Base(){ public Base run(){ return n; } }; } } public class CircularReferences{ public static void

Java中private、public、static、protetced、abstract、final

岁酱吖の 提交于 2020-01-14 19:57:01
abstract (抽象的) 1.abstract可以修饰类和成员方法,被abstract修饰的类称为抽象类,被abstract修饰成员方法叫抽象方法.抽象类不一定有抽象方法,但拥有抽象方法的一定是抽象类; 2.被abstract修饰的类不能直接实例化,需要通过子类实现,所有抽象类一定有子类. 3.继承抽象类的子类必须重写抽象类中的被abstract修饰的抽象方法,如果不继承就必须把自己变成抽象的子类. final (最终的) final可以修饰类,成员变量,成员方法,局部变量/形参. 2.final 修饰的类不能被继承; 3.final修饰的方法不能被重写. 4.final修饰的成员变量是常量,必须在定义时就给值,并且不能改变,不能被重新赋值. 5.final修饰的局部变量、形参只能接受一次赋值,一旦赋值成功,不能重新复制. static(静态的) 1.static可以修饰成员变量,成员方法;被static修饰的会被当前类的所有对象共享. 2.static修饰的成员变量和成员方法会被放在jvm的静态方法区中,随着类记载,可以使用类名或引用调用. 3.static修饰的成员方法只能调用static修饰的成员变量 private(私有的) 1.private可以修饰成员变量和成员方法; 2.被private修饰成员变量和成员方法; 3.被private修饰成员方法不能被重写

About local final variables in Java

偶尔善良 提交于 2020-01-12 10:41:33
问题 In java Program, parameters which is defined as String in method declaration. But in method definition it is accessed as final String variable. Whether it'll lead to some issues (like security, memory problem)? For Example: Method Declaration join(String a,String b); Method definition public void join(final String a,final String b) { Authenticator au = new Authenticator(){ public PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(a,b)} }; } Please help for

About local final variables in Java

被刻印的时光 ゝ 提交于 2020-01-12 10:41:24
问题 In java Program, parameters which is defined as String in method declaration. But in method definition it is accessed as final String variable. Whether it'll lead to some issues (like security, memory problem)? For Example: Method Declaration join(String a,String b); Method definition public void join(final String a,final String b) { Authenticator au = new Authenticator(){ public PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(a,b)} }; } Please help for

Java and the use of the Final keyword

大城市里の小女人 提交于 2020-01-12 09:16:36
问题 I'm new to Java having programmed in Delphi and C# for some time,. My question relates to the usage of the "final" keyword on a variable that holds an instantiated class when the variable declaration and instantiation both happen within the scope of the same method. e.g. private String getDeviceID() { //get the android device id final TelephonyManager tm = (TelephonyManager)GetBaseContext().getSystemService(Context.TELEPHONY_SERVICE); final String deviceID = tm.getDeviceId(); // log debug