final

What is the difference in between ''const'' and ''final'' keyword in Dart?

给你一囗甜甜゛ 提交于 2019-11-27 19:57:19
What is the difference between const and final keyword in Dart? There is a post on dart's website and it explains it pretty well. Final: "final" means single-assignment: a final variable or field must have an initializer. Once assigned a value, a final variable's value cannot be changed. final modifies variables . Const: "const" has a meaning that's a bit more complex and subtle in Dart. const modifies values . You can use it when creating collections, like const [1, 2, 3], and when constructing objects (instead of new) like const Point(2, 3). Here, const means that the object's entire deep

Scala final vs val for concurrency visibility

随声附和 提交于 2019-11-27 19:02:24
In Java, when using an object across multiple threads (and in general), it is good practice to make fields final. For example, public class ShareMe { private final MyObject obj; public ShareMe(MyObject obj) { this.obj = obj; } } In this case, the visibility of obj will be consistent across multiple threads (let's assume obj has all final fields as well) since it is safely constructed using the final keyword. In scala, it doesn't appear val compiles down to a final reference, but rather val is semantics in scala that prevents you from reassigning a variable ( Scala final variables in

Java - Can final variables be initialized in static initialization block?

一个人想着一个人 提交于 2019-11-27 18:57:41
Based on my understanding of the Java language, static variables can be initialized in static initialization block . However, when I try to implement this in practice ( static variables that are final too), I get the error shown in the screenshot below: SyntaxT3rr0r Yes of course: static final variables can be initialized in a static block but .... you have implicit GOTOs in that example ( try/catch is essentially a 'GOTO catch if something bad happens' ). If an exception is thrown your final variables will not be initialized. Note that the use of static constructs goes against Object-Oriented

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

喜夏-厌秋 提交于 2019-11-27 18:55:12
问题 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

Could a final variable be reassigned in catch, even if assignment is last operation in try?

混江龙づ霸主 提交于 2019-11-27 18:17:02
I am quite convinced that here final int i; try { i = calculateIndex(); } catch (Exception e) { i = 1; } i cannot possibly have already been assigned if control reaches the catch-block. However, Java compiler disagrees and claims the final local variable i may already have been assigned . Is there still some subtlety I am missing here, or is this just a weakness of the model used by the Java Language Specification to identify potential reassignments? My main worry are things like Thread.stop() , which may result in an exception being thrown "out of thin air," but I still don't see how it could

How does “final int i” work inside of a Java for loop?

一个人想着一个人 提交于 2019-11-27 18:16:21
I was surprised to see that the following Java code snippet compiled and ran: for(final int i : listOfNumbers) { System.out.println(i); } where listOfNumbers is an array of integers. I thought final declarations got assigned only once. Is the compiler creating an Integer object and changing what it references? Imagine that shorthand looks a lot like this: for (Iterator<Integer> iter = listOfNumbers.iterator(); iter.hasNext(); ) { final int i = iter.next(); { System.out.println(i); } } See @TravisG for an explanation of the scoping rules involved. The only reason why you would use a final loop

Question about local final variable in Java

情到浓时终转凉″ 提交于 2019-11-27 18:13:36
问题 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)? 回答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

Cannot reference “X” before supertype constructor has been called, where x is a final variable

限于喜欢 提交于 2019-11-27 17:47:22
Consider the following Java class declaration: public class Test { private final int defaultValue = 10; private int var; public Test() { this(defaultValue); // <-- Compiler error: cannot reference defaultValue before supertype constructor has been called. } public Test(int i) { var = i; } } The code will not compile, with the compiler complaining about the line I've highlighted above. Why is this error happening and what's the best workaround? Amr Bekhit The reason why the code would not initially compile is because defaultValue is an instance variable of the class Test , meaning that when an

Is there ever a reason to not use the final keyword when catching an exception?

筅森魡賤 提交于 2019-11-27 17:34:21
问题 I've seen some code as below in some example BlackBerry Java classes: try { // stuff that will throw an exception } catch(final Exception e) { // deal with it } I presume the final is for performance. As per the title, since there's rarely (ever?) any reason to modify an Exception that's already been thrown, should they always be final ? If so, isn't this something that could be done by the compiler? Or is it done by the compiler and adding the final manually has no impact at all? 回答1: The

Difference between final static and static final

☆樱花仙子☆ 提交于 2019-11-27 17:00:32
I found a code where it declared code like private final static String API_RTN_SUCCESS = "0"; private final static String API_RTN_ERROR = "1"; public static final String SHARED_PREFERENCE_CONFIG = "shared_preference_config"; public static final String STARTUP_SETTING_KEY = "startup_setting"; What is the difference between them or are they same? Or does it differ for private or public ? Gergely Szilagyi No difference at all. According to 8.3.1 - Classes - Field Modifiers of the Java Language Specification , If two or more (distinct) field modifiers appear in a field declaration, it is customary