final

final class in c++

爱⌒轻易说出口 提交于 2019-11-29 12:08:42
问题 class Temp { private: ~Temp() {} friend class Final; }; class Final : virtual public Temp { public: void fun() { cout<<"In base"; } }; class Derived : public Final { }; void main() { Derived obj; obj.fun(); } The above code tries to achieve non-inheritable class (final). But using above code the object of derived can still be created, why? The desired functionality is achieved only if ctor made private, my question is why it is not achievable in case of dtor private? 回答1: Well, for this

Java final keyword for variables

谁都会走 提交于 2019-11-29 11:26:41
问题 How does the final keyword not make a variable immutable? Wikipedia says it doesn't. 回答1: In Java, the term final refers to references while immutable refers to objects. Assigning the final modifier to a reference means it cannot change to point to another object, but the object itself can be modified if it is mutable. For example: final ArrayList<String> arr = new ArrayList<String>(); arr.add("hello"); // OK, the object to which arr points is mutated arr = null; // Not OK, the reference is

What's the point of declaring an object as “final”?

给你一囗甜甜゛ 提交于 2019-11-29 10:35:59
问题 I just noticed that it's possible to declare objects as final in Scala: final object O What's the point of doing that? One cannot inherit from objects anyway: object A object B extends A // not found: type A 回答1: Not that anyone does this, but: $ scala -Yoverride-objects Welcome to Scala version 2.11.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_11). Type in expressions to have them evaluated. Type :help for more information. scala> trait A { object O } ; trait B extends A { override

Equivalent of const(C++) in Java

风流意气都作罢 提交于 2019-11-29 09:09:02
I was wondering if there was an equivalent to c++'s const in Java. I understand the final keyword, but unfortunately I cannot use that to declare a functions return value final. Instead, it always ensures the function cannot be overridden, correct? Basically, I want to make sure a given returned class cannot be modified and is read only. Is that possible in Java? Basically, I want to make sure a given returned class cannot be modified and is read only. Is that possible in Java? Not directly, but one workaround is an immutable object . Example - public final Foo{ private final String s; public

final variables are not functioning well in jshell

不问归期 提交于 2019-11-29 07:12:14
I am working with jshell of JDK9. I just created a final variable and assigned a value to it. And in the next line i just modified the value. And to my surprise, there was no error when modifying the final variables. Here is the code snippets: jshell> final int r = 0; | Warning: | Modifier 'final' not permitted in top-level declarations, ignored | final int r = 0; | ^---^ r ==> 0 jshell> r = 1; r ==> 1 jshell> System.out.println("r = "+r) r = 1 Is it what is expected from jshell? or there is some other way to work with final variables in jshell? While creating a final variable at the top-level

Where are Java final local variables stored?

﹥>﹥吖頭↗ 提交于 2019-11-29 07:01:15
问题 Take the following example: public void init() { final Environment env = new Environment(); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { env.close(); } }); } Firstly, where is env stored? Is it: copied by the compiler into a hidden member variable of the inner class that references it copied to, and referenced on, the heap left on the stack and somehow referenced there something else My guess is the first option. Secondly, do any performance issues that arise from

final和static关键字

≯℡__Kan透↙ 提交于 2019-11-29 06:27:34
final和static关键字 (1)final关键字 final可以修饰成员变量,表示成员变量初始化后不可修改,变为常量。 final可以修饰方法,表示该方法不可被重写 final可以修饰类,表示该类不可以被继承 final可以修饰局部变量,表示该局部变量在初始化后,转变为常量。 (2)static关键字 static可以修饰成员变量、方法、代码块、内部类 static修饰的属性的初始化在编译时期(类加载的时候),初始化后可以修改(这点是与final的较大区别) static修饰的成员变量、方法在不创建类的对象时也可被访问,与类的具体对象无关 (3)static final static final修饰的属性表示一旦给值,就不可修改,并且可以通过类名访问 来源: https://blog.csdn.net/zys994612/article/details/100540920

How does the compiler benefit from C++'s new final keyword?

喜欢而已 提交于 2019-11-29 04:59:24
问题 C++11 will allow to mark classes and virtual method to be final to prohibit deriving from them or overriding them. class Driver { virtual void print() const; }; class KeyboardDriver : public Driver { void print(int) const final; }; class MouseDriver final : public Driver { void print(int) const; }; class Data final { int values_; }; This is very useful, because it tells the reader of the interface something about the intent of the use of this class/method. That the user gets diagnostics if he

Why can't a Java class be both abstract and final

时光总嘲笑我的痴心妄想 提交于 2019-11-29 04:51:53
Suppose I've a utility class which contains only static methods and variables. e.g: public abstract final class StringUtils { public static final String NEW_LINE = System.getProperty("line.separator"); public static boolean isNotNullOrSpace(final String string) { return !(string == null || string.length() < 1 || string.trim().length() < 1); } } In this scenario, it makes sense to make the class both abstract and final. Abstract because making an object of this class will be of no use as all methods are accessible statically. Final because the derived class cannot inherit anything from this

Question about local final variable in Java

↘锁芯ラ 提交于 2019-11-29 04:18:57
I have the following code: public class BookLib { void f() { final int x = 5; // Line 1 class MyCLass { void print() { System.out.println(x); } } } } I don't understand why should use final variable in this case (Line 1)? You've created an inner class here. Since the life-time of objects of this class can potentially be much greater than the runtime of the method invocation (i.e. the object can still exist long after the method has returned), it needs to "preserve" the state of local variables that it can access. This preserving is done by creating an (invisible, synthetic) copy inside the