final

Is there any sense in marking a base class function as both virtual and final? [duplicate]

喜欢而已 提交于 2019-12-01 03:27:26
This question already has an answer here: What's the point of a final virtual function? 10 answers In various explanations of C++11's final keyword, I'm seeing examples like this. class base { public: virtual void f() final; }; class derived : public base { public: virtual void f(); // Illegal due to base::f() declared final. }; Is this actually a useful use of final ? Why would you declare a virtual function in a base class (implying that it will be usefully overrideable in derived classes) and then immediately mark it as final (negating that implication)? What is the utility of virtual void

Why can I anonymously subclass an enum but not a final class?

隐身守侯 提交于 2019-12-01 03:06:07
This code: public class Sandbox { public enum E { VALUE { @Override public String toString() { return "I'm the value"; } }; @Override public String toString() { return "I'm the enum"; } } public static void main(String[] args) { System.out.println(E.VALUE); } } prints: I'm the value However, this code: public class Sandbox { public static final class C { @Override public String toString() { return "I'm a C"; } } public static void main(String[] args) { System.out.println(new C() { @Override public String toString() { return "I'm anonymous"; } }); } } results in a compilation error: cannot

Do javac or Hotspot automatically add 'final' as an optimisation of invariant variables?

爱⌒轻易说出口 提交于 2019-12-01 03:06:00
问题 The consensus seems to be that there is a performance benefit to marking member variables as final because they never need reloading from main memory. My question is, do javac or Hotspot automatically do this for me when it's obvious the variable cannot change. eg will javac make 'x' final in this class below... public class MyClass { private String x; MyClass(String x) { this.x = x; } public String getX() { return x; } } On a secondary point, has anyone produced empirical evidence that

When should I use “final”?

China☆狼群 提交于 2019-12-01 03:02:11
Is there any particular reason I should declare a class or a method final in day-to-day programming (web or otherwise)? Please provide a real-world example where it should be used. BTW, I'm asking because I'm trying to pick an 'obscure' keyword and master it . It prevents other programmers from doing stuff with your classes that you don't intend for them to do. So rather than making a comment saying "don't use this class to do XXX", you can design it in such a way that they won't be tempted to override it and abuse it in that manner. Edit : Since an example is requested, I'll describe a case

final关键字

自闭症网瘾萝莉.ら 提交于 2019-12-01 02:59:32
final关键字 * final : 最终的 * final 可以用来修饰的结构: 类, 方法, 变量 * * final 修饰一个类 * > 这个类就是最终的类 不可以被其他类所继承 * eg : String 类 System类 StringBuffer类 * * * final 修饰一个方法: 表明此方法不可以被重写 * * * final 修饰变量: 表明此"变量"就称为常量 * final修饰一个属性: 可以考虑赋值的位置有:显示初始化, 代码块初始化, 构造器初始化 final修饰的变量 初始化 位置: eg : public class FinalTest { // final int winth; // 错误, 不可以使用这种方法 final int WIDTH = 9; final int LEFFT; final String name; { // 代码块中赋值 LEFFT = 14; } public FinalTest(){ name = "老王"; } public FinalTest(String n){ name = n; } public static void main(String[] args) { FinalTest finalTest = new FinalTest(); System.out.println(finalTest.name)

Does the Java 'final' keyword actually improve security?

北慕城南 提交于 2019-12-01 02:18:44
While there are many reasons to use the 'final' keyword in Java , one of the ones I keep hearing over and over again is that it makes your code more secure. While this seems to make sense in this trivial case: public class Password { public final String passwordHash; ... } With the final keyword, you would expect that no malicious code would be able to change the variable passwordHash. However, using reflection it is possible to change the final modifier on the passwordHash field. So does 'final' provide any real security, or is it just placebo? Edit: There is some really interesting

Final keyword in method signatures [duplicate]

╄→гoц情女王★ 提交于 2019-12-01 01:56:16
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Final arguments in interface methods - what’s the point? While trying to experiment a few things, I've ran into a problem that it's described in this page. interface B { public int something(final int a); } abstract class C { public int other(final int b); } class A extends C implements B { public int something(int a) { return a++; } public int other(int b) { return b++ } } Why is such feature possible? I don't

Is there any sense in marking a base class function as both virtual and final? [duplicate]

醉酒当歌 提交于 2019-12-01 01:03:25
问题 This question already has answers here : What's the point of a final virtual function? (10 answers) Closed 2 years ago . In various explanations of C++11's final keyword, I'm seeing examples like this. class base { public: virtual void f() final; }; class derived : public base { public: virtual void f(); // Illegal due to base::f() declared final. }; Is this actually a useful use of final ? Why would you declare a virtual function in a base class (implying that it will be usefully

Why can I anonymously subclass an enum but not a final class?

那年仲夏 提交于 2019-11-30 23:52:59
问题 This code: public class Sandbox { public enum E { VALUE { @Override public String toString() { return "I'm the value"; } }; @Override public String toString() { return "I'm the enum"; } } public static void main(String[] args) { System.out.println(E.VALUE); } } prints: I'm the value However, this code: public class Sandbox { public static final class C { @Override public String toString() { return "I'm a C"; } } public static void main(String[] args) { System.out.println(new C() { @Override

Does the Java 'final' keyword actually improve security?

天涯浪子 提交于 2019-11-30 21:47:21
问题 While there are many reasons to use the 'final' keyword in Java, one of the ones I keep hearing over and over again is that it makes your code more secure. While this seems to make sense in this trivial case: public class Password { public final String passwordHash; ... } With the final keyword, you would expect that no malicious code would be able to change the variable passwordHash. However, using reflection it is possible to change the final modifier on the passwordHash field. So does