final

Why won't declaring an array final make it immutable in Java?

流过昼夜 提交于 2019-12-03 06:09:26
Why won't declaring an array final make it immutable in Java? Doesn't declaring something final mean it can't be changed? From question related to immutable array it's clear that declaring an array final doesn't make it unchangeable. The following is possible. final int[] array = new int[] {0, 1, 2, 3}; array[0] = 42; My question is: What is the function of declaring final here then? final is only about the reference that is marked by it; there is no such thing as an immutable array in Java. When you say private final int[] xs = new int[20]; you won't be allowed to say xs = new int[10]; later

What is the difference between constant variables and final variables in java?

人走茶凉 提交于 2019-12-03 05:40:14
Please help me understand the difference between constant variables and final variables in Java. I am a bit confused with it. Luigi Cortese Constant is the concept, the property of the variable. final is the java keyword to declare a constant variable. As other people pointed out, from a semantic/linguistic point of view the expression constant variable is an oxymoron and, as such, we could argue about its correctness. Quoting the specification , anyway, we can read A variable of primitive type [...], that is final and initialized with a compile-time constant expression (§15.28), is called a

Java final abstract class

只谈情不闲聊 提交于 2019-12-03 05:31:49
问题 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

Is final used for optimization in C++?

人走茶凉 提交于 2019-12-03 05:28:09
问题 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 ? 回答1: 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

local variable is accessed within inner class (java)

你。 提交于 2019-12-03 04:58:55
I got two errors after I compiled my code. The errors are: 1. local variable input is accessed within inner class; needs to be declared final String name = input.getText(); 2. local variable c_age is accessed within inner class; needs to be declared final Object child_age = c_age.getSelectedItem(); This is my code: import javax.swing.*; import java.awt.event.*; public class GUI { public static void main(String[] args) { JFrame frame = new JFrame("Try GUI"); JLabel l1 = new JLabel("Please Enter Your Child's Name"); JTextField input = new JTextField("",10); JLabel l2 = new JLabel("Choose Your

Should be logger always final and static?

假装没事ソ 提交于 2019-12-03 04:57:50
Class can be accessed from many threads. Must be logger in this case also be final and static? Thanks. All major java logging packages ( java.util.logging , log4j , etc.) are synchronized and thread safe. The standard pattern of a private final static logger per class is fine even if the class is called from multiple threads. Yes the logger should be static and final. Also preferably private. There needs be only one logger instance per class and also unless you are going to change the log preference dynamically, it is better to make it final. Logger are thread safe and you do not have to worry

Difference between final keyword, finally block and finalized method in java throught one good example [duplicate]

馋奶兔 提交于 2019-12-03 04:07:51
This question already has answers here : In Java, what purpose do the keywords `final`, `finally` and `finalize` fulfil? (6 answers) Often these keywords confuse me. Can any one tell me exactly what the difference between them is? final keyword class On a class it means you forbid to have a child class extending yours. public final class finalClass Attribute/Field final MyObject value = new MyObject() means you won't be able to modify the instance of the object. value = xxxx won't be allowed, but you still can modify the object itself value.field = "xxx"; Method When you use final on a method,

The final local variable cannot be assigned, since it is defined in an enclosing type

时光毁灭记忆、已成空白 提交于 2019-12-03 02:57:40
ratingS = new JSlider(1, 5, 3); ratingS.setMajorTickSpacing(1); ratingS.setPaintLabels(true); int vote; class SliderMoved implements ChangeListener { public void stateChanged(ChangeEvent e) { vote = ratingS.getValue(); } } ratingS.addChangeListener(new SliderMoved()); If i write the above code Eclipse tells me this: Cannot refer to a non-final variable vote inside an inner class defined in a different method But if i add final before int vote it gives me this error: The final local variable vote cannot be assigned, since it is defined in an enclosing type So, how to solve? Well, the standard

`final` keyword equivalent for variables in Python?

拈花ヽ惹草 提交于 2019-12-03 02:57:14
问题 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. 回答1: 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

Why can an anonymous class access non-final class member of the enclosing class

南笙酒味 提交于 2019-12-03 02:12:31
We know that only final local variables can be accessed in an anonymous class, and there is a good reason here: Why are only final variables accessible in anonymous class? . However, I found that an anonymous class can still access a non-final variable if the variable is an member field of the enclosing class: How can I access enclosing class instance variables from inside the anonymous class? I am confused. We ensure that only a final local variable can be accessed in anonymous class because we don't want that the variable should be out-of-sync between anonymous class and local function. The