final

Do the ‘up to date’ guarantees for values of Java's final fields extend to indirect references?

一世执手 提交于 2019-12-19 05:32:30
问题 The Java language spec defines semantics of final fields in section 17.5: The usage model for final fields is a simple one. Set the final fields for an object in that object's constructor. Do not write a reference to the object being constructed in a place where another thread can see it before the object's constructor is finished. If this is followed, then when the object is seen by another thread, that thread will always see the correctly constructed version of that object's final fields.

When should I use “final”?

丶灬走出姿态 提交于 2019-12-19 05:32:09
问题 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. 回答1: 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

Cannot declare Public static final String s = new String(“123”) inside an inner class

蹲街弑〆低调 提交于 2019-12-19 02:49:19
问题 I tried to declare a class as shown below class Outer{ private final class Inner{ public static final String s1 = new String("123"); public static final byte[] bytes = new byte[]{0x00, 0x01}; public static final String s2 = "123"; public static final byte byte1 = 0x02; } } In the above code s1 and bytes wont compile but s2 and byte1 compile. If I put the whole constant declaration in outer class it works fine. what am i missing. Any help? 回答1: Read Java Language Specification, 3rd ed, §8.1.3.

Using the non final loop variable inside a lambda expression

ぐ巨炮叔叔 提交于 2019-12-18 19:08:38
问题 I'm trying to execute 100 tasks all in parallel via executors and runnable, the task needs to use the loop variable: for (int i = 0; i < 100; i++) { executor.execute(() -> { doSomething(String.format("Need task number %d done", i)); } }); } I get a squiggly under 'i' saying - Variable used in lambda expression should be effectively final. A loop variable, as far as I'm aware, cannot be made final or effectively final, since it is being changed with each iteration. I found a simple workaround,

public static final variable in an imported java class

笑着哭i 提交于 2019-12-18 14:19:14
问题 I happen to come across a Java code at my work place. Here's the scenario: There are 2 classes - ClassA and ClassB . ClassA has nothing except 4 public static final string values inside it. Its purpose is to use those values like ClassA.variable (don't ask me why, it's not my code). ClassB imports ClassA . I edited the string values in ClassA and compiled it. When I ran ClassB I could see it was using the old values - not the new values. I had to recompile ClassB to make it use new values

Is “public static final” redundant for a constant in a Java interface?

允我心安 提交于 2019-12-18 12:07:19
问题 This code: interface Config { int MAX_CONN = 20; } compiled and worked as I expected. It looks like this is the same as: interface Config { public static final int MAX_CONN = 20; } Is "public static final" redundant for a constant in a Java interface? Is this true for Java 1.1, 1.2, 1.3, 1.4,..., 1.8 or did it change in a Java release? 回答1: Variables declared in Interface are implicitly public static final . This is what JLS 9.3 says : Every field declaration in the body of an interface is

'public static final' or 'private static final' with getter?

霸气de小男生 提交于 2019-12-18 10:02:52
问题 In Java, it's taught that variables should be kept private to enable better encapsulation, but what about static constants? This: public static final int FOO = 5; Would be equivalent in result to this: private static final int FOO = 5; ... public static getFoo() { return FOO; } But which is better practice? 回答1: There's one reason to not use a constant directly in your code. Assume FOO may change later on (but still stay constant), say to public static final int FOO = 10; . Shouldn't break

Purpose of final and sealed

余生长醉 提交于 2019-12-18 05:48:20
问题 Why would anyone want to mark a class as final or sealed? 回答1: According to Wikipedia, "Sealed classes are primarily used to prevent derivation. They add another level of strictness during compile-time, improve memory usage, and trigger certain optimizations that improve run-time efficiency." Also, from Patrick Smacchia's blog: Versioning: When a class is originally sealed, it can change to unsealed in the future without breaking compatibility. (…) Performance: (…) if the JIT compiler sees a

final variables are not functioning well in jshell

好久不见. 提交于 2019-12-18 05:02:23
问题 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

final variables are not functioning well in jshell

与世无争的帅哥 提交于 2019-12-18 05:01:02
问题 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