final

Java final abstract class

情到浓时终转凉″ 提交于 2019-12-02 19:58:00
I have a quite simple question: I want to have a Java Class, which provides one public static method, which does something. This is just for encapsulating purposes (to have everything important within one separate class)... This class should neither be instantiated, nor being extended. That made me write: final abstract class MyClass { static void myMethod() { ... } ... // More private methods and fields... } (though I knew, it is forbidden). I also know, that I can make this class solely final and override the standard constructor while making it private. But this seems to me more like a

内部类访问局部变量时,为什么需要加final关键字

六眼飞鱼酱① 提交于 2019-12-02 18:51:30
是变量的作用域的问题,因为匿名内部类是出现在一个方法的内部的,如果它要访问这个方法的参数或者方法中定义的变量,则这些参数和变量必须被修饰为final。因为虽然匿名内部类在方法的内部,但实际编译的时候,内部类编译成Outer.Inner,这说明内部类所处的位置和外部类中的方法处在同一个等级上,外部类中的方法中的变量或参数只是方法的局部变量,这些变量或参数的作用域只在这个方法内部有效。因为编译的时候内部类和方法在同一级别上,所以方法中的变量或参数只有为final,内部类才可以引用。 Java代码: package com.cxz.j2se; public class MyClass { public MyClass() { final int finalValue = 10; int not$Final = 20; MyInterface myInterface = new MyInterface() { public void functionWithoutPara() { //compile Error //System.out.println(noFinal); System.out.println(finalValue); } public void functionWithPara(int num) { System.out.println("The parameter "

Is final used for optimization in C++?

我们两清 提交于 2019-12-02 18:46:50
class A { public: virtual void f() = 0; }; class B : public A { public: void f() final override { }; }; int main() { B* b = new B(); b->f(); } In this case, is the compiler required to still do the v-table lookup for b->f(); , or can it call B::f() directly because it was marked final ? Niall Is final used for optimization in C++? It can be, and is. As noted, it is being used already; see here and here showing the generated code for the override with and without final . An optimisation along these lines would relate to the "de-virtualization" of the virtual calls. This is not always

Java: startingPath as “public static final” exception

安稳与你 提交于 2019-12-02 17:49:16
问题 [Updated, sorry about the change but now to the real problem] I cannot include try-catch-loop there for the exception from the method getCanonicalPath(). I tried to solve the problem earlier with method and then declaring the value there. The problem is that it is Final, I am unable to change it. So how to have startingPath as "public static final". $ cat StartingPath.java import java.util.*; import java.io.*; public class StartingPath { public static final String startingPath = (new File("."

Java之路---Day13

試著忘記壹切 提交于 2019-12-02 17:11:22
2019-10-28-22:40:14 目录   1. Instanceof关键字   2. Final关键字     2.1 Final关键字修饰类     2.2 Final关键字修饰成员方法     2.3 Final关键字修饰局部变量     2.4 Final关键字修饰成员变量   3. 权限修饰符 Instanceof关键字   作用:判断一个父类引用的对象是什么子类   格式:     对象名 instanceof 类名称    1 package demosummary.instanceoftest; 2 3 public class Test { 4 public static void main(String[] args) { 5 //创建一个Dog对象 6 Animal animal = new Dog(); 7 //如果希望调用子类特有方法,需要向下转型 8 //判断一下父类引用animal本来是不是Dog 9 if (animal instanceof Dog){ 10 animal.setName("来福"); 11 Dog dog = (Dog)animal; 12 dog.skill(); 13 } 14 15 } 16 } Final关键字   意义:final关键字代表最终,不可改变的   常见四种用法:        1.可以用来修饰一个类

Does final imply override?

你离开我真会死。 提交于 2019-12-02 16:58:48
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? final does not require the function to override anything in the first place. Its effect is defined in [class.virtual]/4 as If a

`final` keyword equivalent for variables in Python?

时光怂恿深爱的人放手 提交于 2019-12-02 16:58:28
I couldn't find documentation on an equivalent of Java's final in Python, is there such a thing? I'm creating a snapshot of an object (used for restoration if anything fails); once this backup variable is assigned, it should not be modified -- a final-like feature in Python would be nice for this. Having a variable in Java be final basically means that once you assign to a variable, you may not reassign that variable to point to another object. It actually doesn't mean that the object can't be modified. For example, the following Java code works perfectly well: public final List<String>

Is final ill-defined?

浪尽此生 提交于 2019-12-02 14:15:43
First, a puzzle: What does the following code print? public class RecursiveStatic { public static void main(String[] args) { System.out.println(scale(5)); } private static final long X = scale(10); private static long scale(long value) { return X * value; } } Answer: 0 Spoilers below. If you print X in scale(long) and redefine X = scale(10) + 3 , the prints will be X = 0 then X = 3 . This means that X is temporarily set to 0 and later set to 3 . This is a violation of final ! The static modifier, in combination with the final modifier, is also used to define constants. The final modifier