final

Does using final for variables in Java improve garbage collection?

纵然是瞬间 提交于 2019-11-26 10:18:52
问题 Today my colleagues and me have a discussion about the usage of the final keyword in Java to improve the garbage collection. For example, if you write a method like: public Double doCalc(final Double value) { final Double maxWeight = 1000.0; final Double totalWeight = maxWeight * value; return totalWeight; } Declaring the variables in the method final would help the garbage collection to clean up the memory from the unused variables in the method after the method exits. Is this true? 回答1:

Can a static method be overridden in C#?

大兔子大兔子 提交于 2019-11-26 09:40:58
问题 I was told that static methods are implicitly final and therefore can\'t be overridden. Is that true? Can someone give a better example of overriding a static method? If static methods are just class methods, what is the real use of having them? 回答1: (1) Static methods cannot be overridden, they can however be hidden using the 'new' keyword. Mostly overriding methods means you reference a base type and want to call a derived method. Since static's are part of the type and aren't subject to

Why does the Java compiler not understand this variable is always initialized?

六眼飞鱼酱① 提交于 2019-11-26 09:32:50
问题 class Foo{ public static void main(String args[]){ final int x=101; int y; if(x>100){ y=-1; } System.out.println(y); } } Java compiler understands the condition of the if statement is always true and therefore y will always be initialized. No compile error, as expected. class Bar{ public static void main(String args[]){ final int x; x=101; int y; if(x>100){ y=-1; } System.out.println(y); } } But when I break the declaration and initialization of x into two lines, the compiler does not seem to

What is the difference between the “const” and “final” keywords in Dart?

[亡魂溺海] 提交于 2019-11-26 09:28:50
问题 What is the difference between const and final keyword in Dart? 回答1: 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

what is the sense of final ArrayList?

别来无恙 提交于 2019-11-26 09:24:23
问题 Which advantages/disadvantages we can get by making ArrayList (or other Collection) final? I still can add to ArrayList new elements, remove elements and update it. But what is effect making it\'s final? 回答1: But what is effect making it's final? This means that you cannot rebind the variable to point to a different collection instance : final List<Integer> list = new ArrayList<Integer>(); list = new ArrayList<Integer>(); // Since `list' is final, this won't compile As a matter of style, I

in what order are static blocks and static variables in a class executed? [duplicate]

情到浓时终转凉″ 提交于 2019-11-26 08:56:31
问题 Possible Duplicate: Java static class initialization Why is the string variable updated in the initialization block and not the integer(even though the block is written first) class NewClass { static { System.out.println(NewClass.string+\" \"+NewClass.integer); } final static String string=\"static\"; final static Integer integer=1; public static void main(String [] args)//throws Exception { } } My output is static null P.S:Also noticed that string variable initialization happens before the

Why are `private val` and `private final val` different?

蓝咒 提交于 2019-11-26 08:48:52
问题 I used to think that private val and private final val are same, until I saw section 4.1 in Scala Reference: A constant value definition is of the form final val x = e where e is a constant expression (§6.24). The final modifier must be present and no type annotation may be given. References to the constant value x are themselves treated as constant expressions; in the generated code they are replaced by the definition’s right-hand side e. And I have written a test: class PrivateVal { private

Why is there no Constant feature in Java?

冷暖自知 提交于 2019-11-26 08:47:41
问题 I was trying to identify the reason behind constants in Java I have learned that Java allows us to declare constants by using final keyword. My question is why didn\'t Java introduce a Constant ( const ) feature. Since many people say it has come from C++, in C++ we have const keyword. Please share your thoughts. 回答1: Every time I go from heavy C++ coding to Java, it takes me a little while to adapt to the lack of const-correctness in Java. This usage of const in C++ is much different than

Changing private final fields via reflection

橙三吉。 提交于 2019-11-26 08:10:20
问题 class WithPrivateFinalField { private final String s = \"I’m totally safe\"; public String toString() { return \"s = \" + s; } } WithPrivateFinalField pf = new WithPrivateFinalField(); System.out.println(pf); Field f = pf.getClass().getDeclaredField(\"s\"); f.setAccessible(true); System.out.println(\"f.get(pf): \" + f.get(pf)); f.set(pf, \"No, you’re not!\"); System.out.println(pf); System.out.println(f.get(pf)); Output: s = I’m totally safe f.get(pf): I’m totally safe s = I’m totally safe No