final

Scala - initialization order of vals

心已入冬 提交于 2019-11-26 18:29:04
问题 I have this piece of code that loads Properties from a file: class Config { val properties: Properties = { val p = new Properties() p.load(Thread.currentThread().getContextClassLoader.getResourceAsStream("props")) p } val forumId = properties.get("forum_id") } This seems to be working fine. I have tried moving the initialization of properties into another val, loadedProperties , like this: class Config { val properties: Properties = loadedProps val forumId = properties.get("forum_id") private

Why can final object be modified?

人盡茶涼 提交于 2019-11-26 18:24:29
I came across the following code in a code base I am working on: public final class ConfigurationService { private static final ConfigurationService INSTANCE = new ConfigurationService(); private List providers; private ConfigurationService() { providers = new ArrayList(); } public static void addProvider(ConfigurationProvider provider) { INSTANCE.providers.add(provider); } ... INSTANCE is declared as final . Why can objects be added to INSTANCE ? Shouldn't that invalidate the use of final. (It doesn't). I'm assuming the answer has to do something with pointers and memory but would like to

Should a “static final Logger” be declared in UPPER-CASE?

做~自己de王妃 提交于 2019-11-26 18:03:42
In Java, static final variables are constants and the convention is that they should be in upper-case. However, I have seen that most people declare loggers in lower-case which comes up as a violation in PMD . e.g: private static final Logger logger = Logger.getLogger(MyClass.class); Just search google or SO for "static final logger" and you will see this for yourself. Should we be using LOGGER instead? The logger reference is not a constant, but a final reference, and should NOT be in uppercase. A constant VALUE should be in uppercase. private static final Logger logger = Logger.getLogger

What is the purpose of the “final” keyword in C++11 for functions?

大憨熊 提交于 2019-11-26 17:56:24
问题 What is the purpose of the final keyword in C++11 for functions? I understand it prevents function overriding by derived classes, but if this is the case, then isn't it enough to declare as non-virtual your final functions? Is there another thing I'm missing here? 回答1: What you are missing, as idljarn already mentioned in a comment is that if you are overriding a function from a base class, then you cannot possibly mark it as non-virtual: struct base { virtual void f(); }; struct derived :

小白之旅10

大城市里の小女人 提交于 2019-11-26 17:37:05
一. final关键字 final修饰变量,变成常量,常量不能修改 注:如果final修饰的是成员变量,那么这个成员变量不会赋予默认值,必须手动初始化 被final修饰的对象不能被重新赋值 被final修饰的类是最终类,最终类不能被继承 注:final与abstract不能共存,因为抽象类必须被继承才有意义,而final修饰的最终类无法被继承 被final修饰的方法不能被子类重写 二. 包和导入 关键字:package、import package:表示当前类所属的包 import:如果要使用其他包中的类,需要将这个类的包结构通过import导入到当前类中 注: 1、import可以使用星号按需导入,例如:import java.util.*;表示系统会根据当前类中所使用的util包中的类进行导入 2、lang包无需手动导入,每个类都已经自动导入了lang语言包 三. 权限(范围)修饰符 概念:用于修饰成员的可见度 有public protected [default] private 注:权限修饰符只能修饰成员,不能修饰局部 public protected [default] private 本类 √ √ √ √ 同包不同类 √ √ √ × 不同包的子类 √ √ × × 不同包的无关类 √ × × × 不同范围修饰符的使用规则: 1、public:工具类、对外提供的功能 2

Testing initialization safety of final fields

不羁的心 提交于 2019-11-26 17:36:13
问题 I am trying to simply test out the initialization safety of final fields as guaranteed by the JLS. It is for a paper I'm writing. However, I am unable to get it to 'fail' based on my current code. Can someone tell me what I'm doing wrong, or if this is just something I have to run over and over again and then see a failure with some unlucky timing? Here is my code: public class TestClass { final int x; int y; static TestClass f; public TestClass() { x = 3; y = 4; } static void writer() {

Making java method arguments as final

这一生的挚爱 提交于 2019-11-26 17:35:04
问题 What difference that final makes between the code below. Is there any advantage in declaring the arguments as final . public String changeTimezone( Timestamp stamp, Timezone fTz, Timezone toTz){ return .... } public String changeTimezone(final Timestamp stamp, final Timezone fTz, final Timezone toTz){ return .... } 回答1: As a formal method parameter is a local variable, you can access them from inner anonymous classes only if they are declared as final. This saves you from declaring another

Why defining class as final improves JVM performance?

此生再无相见时 提交于 2019-11-26 17:27:44
问题 Quoting from http://sites.google.com/site/gson/gson-design-document: Why are most classes in Gson marked as final? While Gson provides a fairly extensible architecture by providing pluggable serializers and deserializers, Gson classes were not specifically designed to be extensible. Providing non-final classes would have allowed a user to legitimately extend Gson classes, and then expect that behavior to work in all subsequent revisions. We chose to limit such use-cases by marking classes as

Final arguments in interface methods - what's the point?

时间秒杀一切 提交于 2019-11-26 17:26:56
问题 In Java, it is perfectly legal to define final arguments in interface methods and do not obey that in the implementing class, e.g.: public interface Foo { public void foo(int bar, final int baz); } public class FooImpl implements Foo { @Override public void foo(final int bar, int baz) { ... } } In the above example, bar and baz has the opposite final definitions in the class VS the interface. In the same fashion, no final restrictions are enforced when one class method extends another, either

Why Final variable doesn't require initialization in main method in java?

北城余情 提交于 2019-11-26 17:14:34
问题 When I am just trying to do some program in Java .I try to use final variable,I know that final variable must be initialized at the time of declaration, but inside the main method it accepts the final variable with out initialization. I don't know what's the reason.Can any one tell me the reason. Thank you code: class name { final int b; //here shows error public static void main(String args[]) { final int a; // here no error... why? System.out.println("hai"); } } 回答1: For instance variable