final

Why can I still make changes to this final variable? [duplicate]

纵然是瞬间 提交于 2019-12-10 10:20:18
问题 This question already has answers here : Final variable manipulation in Java (10 answers) Closed 4 years ago . If name is declared final , why i can still call name.append and the output is: shreya ? I thought final variables cannot be changed once a value is assigned? public class Test1 { final static StringBuilder name = new StringBuilder("sh"); public static void main(String[] args) { name.append("reya"); System.out.println(name); } } 回答1: final refers to not being able to change the

Reasoning for making a method final

我怕爱的太早我们不能终老 提交于 2019-12-10 03:05:31
问题 Sorry, quick question here, just found something in my notes I don't understand in relation to making a method final. My notes claim you should make a method final for this reason : Makes it impossible to enforce invariants. A String should behave as a String. I don't really understand what is meant by this. Can someone please break it down for me ? Thanks a lot. 回答1: I would guess that should have said "Make it possible to enforce invariants". Basically, if someone can override a method,

What is the purpose of using final for the loop variable in enhanced for loop?

我怕爱的太早我们不能终老 提交于 2019-12-10 01:11:57
问题 I understand how the below statement works. for(final Animal animal : animalList){ //do some function } But what is the purpose of using the final keyword here ? 回答1: There are two possible reasons to do this: It could simply be a way to avoid changing the loop variable accidentally in the loop body. (Or to document the fact that the loop variable is not going to be changed.) It could be done so that you can refer to the loop variable in an anonymous inner class. For example: for(final Animal

Java Applet - Cannot inherit from final class

旧城冷巷雨未停 提交于 2019-12-09 21:14:58
问题 We have a java applet which is working OK in most client environments, primarily Windows 7, but recently we have been asked to support Ubuntu clients as well. The problem is that when the applet is fired up on the Ubuntu client (running Firefox and the natively installed "IcedTEA" Java VM 1.7.0_75) we get this exception: java.lang.VerifyError: Cannot inherit from final class at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:800) at java

Can an abstract class have a final method?

一曲冷凌霜 提交于 2019-12-09 13:44:27
问题 Can an abstract class have a final method in Java? 回答1: Sure. Take a look at the Template method pattern for an example. abstract class Game { protected int playersCount; abstract void initializeGame(); abstract void makePlay(int player); abstract boolean endOfGame(); abstract void printWinner(); /* A template method : */ final void playOneGame(int playersCount) { this.playersCount = playersCount; initializeGame(); int j = 0; while (!endOfGame()) { makePlay(j); j = (j + 1) % playersCount; }

javac treating static final differently based on assignment method

こ雲淡風輕ζ 提交于 2019-12-09 13:07:36
问题 When I compile: public static final boolean FOO = false; public static final void fooTest() { if (FOO) { System.out.println("gg"); } } I get an empty method fooTest() {} . However when I compile: static boolean isBar = false; public static final boolean BAR = isBar; public static final void fooTest() { if (BAR) { System.out.println("gg"); } } the if statement is included in the compiled class file. Does this mean there are two different "types" of static final in java, or is this just a

inner class non-final variable java

此生再无相见时 提交于 2019-12-09 10:32:25
问题 I needed to change variables inside an inner class and I got the infamous "Cannot refer to a non-final variable inside an inner class defined in a different method" error. void onStart(){ bt.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { int q = i; } }); } I quickly made a class that held all of the things I wanted to change and made a final version of the class outside the inner class class temp{ int q; } void onStart(){ final temp x = new temp(); bt

How to detect if a class is final in C++11?

混江龙づ霸主 提交于 2019-12-09 00:22:52
问题 Code first. #include <iostream> using namespace std; struct A final {}; struct B {}; int main() { cout << is_final<A>::value << endl; // Output true cout << is_final<B>::value << endl; // Output false return 0; } How to implement the class is_final? 回答1: Type traits are usually implemented using the SFINAE idiom, which places a potentially ill-formed expression inside a function template declaration. Substituting the typename in question into the declaration results in an error, but the error

Cleanly suppress gcc's `final` suggestion warnings (`-Wsuggest-final-types` and `-Wsuggest-final-methods`)

隐身守侯 提交于 2019-12-08 18:02:26
问题 I like compiling my code using -Wsuggest-final-types and -Wsuggest-final-methods in order to be warned about opportunities where the final keyword could be used to allow the compiler to optimize more aggressively. Sometimes, though, the suggestions are incorrect - for example, I have a class Base with a virtual ~Base() destructor that is used polymorphically in another project, and gcc suggests me that Base could be marked as final . Is there a way to "cleanly" tell the compiler that Base is

what does final mean in Groovy

孤者浪人 提交于 2019-12-08 17:13:44
问题 If you run the following code in the Groovy console it prints "8" class F { private final Integer val = 2 def set(v) {val = v} def print() {println val} } def f = new F() f.set(8) f.print() In Java this code wouldn't compile because you can't assign a final reference after the constructor has run. I know that for properties, final indicates that the property can't be changed outside the class, but what does it mean to mark a private field final ? Thanks, Don 回答1: It looks like this might be a