final

What does a variable being “effectively final” mean? [duplicate]

眉间皱痕 提交于 2019-12-17 16:32:55
问题 This question already has answers here : Difference between final and effectively final (13 answers) Closed 5 years ago . The documentation on Anonymous Classes states An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final. I don't understand what does a variable being "effective final" mean. Can someone provide an example to help me understand what that means? 回答1: Effectively final means that it is never changed after

Java static final values replaced in code when compiling?

笑着哭i 提交于 2019-12-17 09:42:15
问题 In java, say I have the following ==fileA.java== class A { public static final int SIZE = 100; } Then in another file i use this value ==fileB.java== import A; class b { Object[] temp = new Object[A.SIZE]; } When this gets compiled does SIZE get replaced with the value 100, so that if i were to down the road replace the FileA.jar but not FileB.jar would the object array get the new value or would it have been hardcoded to 100 because thats the value when it was originally built? Thanks,

Java static final values replaced in code when compiling?

≡放荡痞女 提交于 2019-12-17 09:41:45
问题 In java, say I have the following ==fileA.java== class A { public static final int SIZE = 100; } Then in another file i use this value ==fileB.java== import A; class b { Object[] temp = new Object[A.SIZE]; } When this gets compiled does SIZE get replaced with the value 100, so that if i were to down the road replace the FileA.jar but not FileB.jar would the object array get the new value or would it have been hardcoded to 100 because thats the value when it was originally built? Thanks,

What's the point of a final virtual function?

拈花ヽ惹草 提交于 2019-12-17 09:37:10
问题 Wikipedia has the following example on the C++11 final modifier: struct Base2 { virtual void f() final; }; struct Derived2 : Base2 { void f(); // ill-formed because the virtual function Base2::f has been marked final }; I don't understand the point of introducing a virtual function and immediately marking it as final. Is this simply a bad example, or is there more to it? 回答1: Typically final will not be used on the base class' definition of a virtual function. final will be used by a derived

creating final variables inside a loop

会有一股神秘感。 提交于 2019-12-17 06:38:37
问题 is this allowed in java: for(int i=0;i<5;i++){ final int myFinalVariable = i; } The keyword of my question is final . Is it allowed to do a final variable that changes with every run of the loop? I was wondering this because final says that you can't change the value of the variable (calling only myFinalVariable = i ), but i'm redefining the whole variable with final int . Are they two completely different variables just with the same name - with the variable from the previous run of the loop

Final variable manipulation in Java

眉间皱痕 提交于 2019-12-17 06:28:56
问题 Could anyone please tell me what is the meaning of the following line in context of Java: final variable can still be manipulated unless it's immutable As far as I know, by declaring any variable as final, you can't change it again, then what they mean with the word immutable in above line? 回答1: It means that if your final variable is a reference type (i.e. not a primitive like int), then it's only the reference that cannot be changed. It cannot be made to refer to a different object, but the

Final variable manipulation in Java

若如初见. 提交于 2019-12-17 06:28:02
问题 Could anyone please tell me what is the meaning of the following line in context of Java: final variable can still be manipulated unless it's immutable As far as I know, by declaring any variable as final, you can't change it again, then what they mean with the word immutable in above line? 回答1: It means that if your final variable is a reference type (i.e. not a primitive like int), then it's only the reference that cannot be changed. It cannot be made to refer to a different object, but the

Java `final` method: what does it promise?

喜欢而已 提交于 2019-12-17 03:25:24
问题 In a Java class a method can be defined to be final , to mark that this method may not be overridden: public class Thingy { public Thingy() { ... } public int operationA() {...} /** this method does @return That and is final. */ public final int getThat() { ...} } That's clear, and it may be of some use to protect against accidental overriding, or maybe performance — but that's not my question. My question is: From an OOP point of view I understood that, by defining a method final the class

Java's final vs. C++'s const

断了今生、忘了曾经 提交于 2019-12-17 03:24:13
问题 The Java for C++ programmers tutorial says that (highlight is my own): The keyword final is roughly equivalent to const in C++ What does "roughly" mean in this context? Aren't they exactly the same? What are the differences, if any? 回答1: In C++ marking a member function const means it may be called on const instances. Java does not have an equivalent to this. E.g.: class Foo { public: void bar(); void foo() const; }; void test(const Foo& i) { i.foo(); //fine i.bar(); //error } Values can be

Behaviour of final static method

不羁岁月 提交于 2019-12-17 03:23:58
问题 I have been playing around with modifiers with static method and came across a weird behaviour. As we know, static methods cannot be overridden, as they are associated with class rather than instance. So if I have the below snippet, it compiles fine //Snippet 1 - Compiles fine public class A { static void ts() { } } class B extends A { static void ts() { } } But if I include final modifier to static method in class A, then compilation fails ts() in B cannot override ts() in A; overridden