final

java中final关键字的作用

点点圈 提交于 2019-11-29 19:17:39
1. 被final关键字修饰的类不能被继承 final class Father{ } public class Son extends Father { } //Son不能继承Father类 2. final 修饰的方法不能被重写 3.final 修饰的变量叫常量,常量必须初始化,初始化之后值就不能被修改 (1) 对于一个final变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改;如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一个对象。   上面的一段代码中,对变量i和引用型String a的重新赋值都报错了,而b没报错。 (2)引用变量被final修饰之后,虽然不能再指向其他对象,但是它指向的对象的内容是可变的。如下解释 public class Test { public static void main(String[] args) { final MyClass myClass = new MyClass(); System.out.println(++myClass.i); } } class MyClass { public int i = 0; } 来源: https://www.cnblogs.com/Hitleren/p/11528855.html

How does “final” play a role in security? [duplicate]

最后都变了- 提交于 2019-11-29 18:02:04
问题 This question already has an answer here: Does the Java 'final' keyword actually improve security? 12 answers I have read in Wikipedia here that: A final class cannot be subclassed. This is done for reasons of security and efficiency. I am wondering about the kind of security that final in Java can achieve? 回答1: final does not provide any security whatsoever. It's silly to expect it to. It provides a kind of safety. Though your question is related to java, I'll point you to Marshall Cline's C

How does python prevent a class from being subclassed? [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-11-29 18:00:20
问题 This question already has an answer here: Final classes in Python 3.x- something Guido isn't telling me? 4 answers I came across the following in the python docs: bool([x]) Convert a value to a Boolean, using the standard truth testing procedure. If x is false or omitted, this returns False; otherwise it returns True. bool is also a class, which is a subclass of int. Class bool cannot be subclassed further. Its only instances are False and True. I've never in my life wanted to subclass bool ,

java: how to declare final a variable that is initialized inside a try - catch block?

我的未来我决定 提交于 2019-11-29 17:37:22
问题 I have a variable that is not supposed to change its value after it's been initialized, so I want to define it as a final variable. the problem is that the variable has to be initialized inside a try block, so I get the following troubles: I have the following code: Connection conn = null; try { conn = getConn(prefix); [...do some stuff with conn...] } catch (Exception e) { throw new DbHelperException("error opening connection", e); } finally { closeConnection(conn); } If I declare the

Java Lambda Expression with Non-final Function Paramter

落爺英雄遲暮 提交于 2019-11-29 17:15:06
I am trying to simply time a function by using the Runnable interface to wrap around whichever function I need. private static double time(Runnable runnable) { // returns how long runnable took long startTime = System.nanoTime(); runnable.run(); return (System.nanoTime() - startTime) / 1000000000.0; } then I could simply do the following: double durationOfFunctionA = time(Driver::functionA); // functionA belongs in class Driver however, if I have a function that takes a parameter, it must be modified to: double durationOfFunctionB = time(() -> functionB(someParameter)); The problem I am having

Java 7 - Precise rethrow with a final Exception

依然范特西╮ 提交于 2019-11-29 16:26:04
问题 In previous versions of java, rethrowing an exception was treated as throwing the type of the catch parameter. For example: public static void test() throws Exception{ DateFormat df = new SimpleDateFormat("yyyyMMdd"); try { df.parse("x20110731"); new FileReader("file.txt").read(); } catch (Exception e) { System.out.println("Caught exception: " + e.getMessage()); throw e; } } In Java 7, you can be more precise about the exception being thrown, if you declare the exception final : //(doesn't

changing final variables through reflection, why difference between static and non-static final variable

ⅰ亾dé卋堺 提交于 2019-11-29 14:28:28
问题 Please refer to the below code. When I run the code, I am able to change the value of a final non-static variable. But if I try to change the value of a final static variable then it throws java.lang.IllegalAccessException . My question is why doesn't it throw an exception in case of non-static final variable also or vice versa. Why the difference? import java.lang.reflect.Field; import java.util.Random; public class FinalReflection { final static int stmark = computeRandom(); final int

Cannot see final variable content inside anonymous class when debugging in Eclipse an Android app

风格不统一 提交于 2019-11-29 13:22:52
问题 When in debug (eclipse), I cannot see variables content in the variables view, nor in Expressions view, nor in Display view - if the variables are defined outside an anonymous class but the debug is inside the anonymous class. When I try to see the content in debug, I get the error: x cannot be resolved to a variable . In the following example, x cannot be resolved: private void someMethod(final Object x) { new Runnable() { public void run() { Log.i(x); // x is printed correctly but cannot be

Does C++ final imply final in all aspects?

怎甘沉沦 提交于 2019-11-29 12:15:00
问题 C++11 added final. Finally! I understand final does two things: Makes a class non-inheritable. Makes (virtual) functions in a class non-overridable (in a derived class). Both of these seem independent of each other. But take for example the following: class Foo { public: virtual void bar() { //do something unimportant. } }; class Baz final : public Foo { public: void bar() /*final*/ override { //do something more important than Foo's bar. } }; From above, I believe Baz being final , I should

cannot assign value to “final” variable in java

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 12:14:46
问题 private void pushButtonActionPerformed(java.awt.event.ActionEvent evt) { final int c=0; final JDialog d=new JDialog(); JLabel l=new JLabel("Enter the Element :"); JButton but1=new JButton("OK"); JButton but2=new JButton("Cancel"); final JTextField f=new JTextField(10); JPanel panel = new JPanel(); but1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { c=Integer.parseInt(f.getText()); d.setVisible(false); d.dispose( ); } }); but2.addActionListener(new