final

How to prevent a function from being overridden in python [duplicate]

孤者浪人 提交于 2019-11-27 03:03:09
问题 This question already has an answer here: Making functions non override-able 5 answers Is there a way to make a class function unoverriddable? something like java's final keyword. i.e, any overriding class cannot override that method. 回答1: The issue is you are trying to write in Python using Java philosophies. Some thing carry over, but not all of them. In Python you can do the following and it is perfectly fine, but completely goes against how Java thinks of objects. class Thing(object): x =

why are java constants declared static?

限于喜欢 提交于 2019-11-27 02:30:35
问题 Why are java constants declared static ? class Foo{ static final int FII = 2 ; } In this I understand the use of final? Buy why does it have to be static? Why should it be a class variable, and not an instance variable? 回答1: If it could vary by the instance of a class, then it's clearly not a constant . What would it mean to get a different value of pi for each instance of Math (not that Math even allows instances to be constructed)? Or a different case insensitive ordering for each instance

immutable class should be final?

狂风中的少年 提交于 2019-11-27 01:57:54
It says in this article that: Making a class final because it is immutable is a good reason to do so. I'm a bit puzzled by this... I understand that immutability is a good thing from the POV of thread-safety and simplicity, but it seems that these concerns are somewhat orthogonal to extensibility. So, why is immutability a good reason for making a class final? The explanation for this is given in the book 'Effective Java' Consider BigDecimal and BigInteger classes in Java . It was not widely understood that immutable classes had to be effectively final when BigInteger and BigDecimal were

Why final instance class variable in Java?

只谈情不闲聊 提交于 2019-11-27 01:53:15
问题 If instance variable is set final its value can not be changed like public class Final { private final int b; Final(int b) { this.b = b; } int getFinal() { return b = 8; // COMPILE TIME ERROR } } Somewhere in code I have seen instance class variable HashMap declared as final private final Map<String, Object> cacheMap = new HashMap<String, Object>(); I could not understand why it is declared so? Normally in which case it is declared. Does it mean if once I put in hash map then I could not

Why it says that “Cannot refer to a non-final variable i inside an inner class defined in a different method”? [duplicate]

北慕城南 提交于 2019-11-27 01:23:05
问题 This question already has an answer here: Cannot refer to a non-final variable inside an inner class defined in a different method 20 answers I have button click listener and in onCreate() method I have a local variable like onCreate() { super.onCreate(); int i = 10; Button button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { i++; } }); Why java asks for to make me final ? 回答1: When the onCreate() method

Java: Meaning of catch (final SomeException e)?

核能气质少年 提交于 2019-11-27 01:15:06
问题 What does final do in the following Java expression? catch (final SomeExceptionType e) 回答1: It basically means: Catch "SomeExceptionType" into the variable "e" with the promise that we won't assign a different exception to "e" during the processing of the exception. Mostly this is overkill, as if I'm catching an exception into a temporary variable name (e only is valid for the exception handling block), I don't have to police myself so strictly as to not trust myself to assign a different

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

大兔子大兔子 提交于 2019-11-27 01:00:31
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 get that the condition is always true and y will always be initialized. final int x; x=101; byte b; b

what is the sense of final ArrayList?

蹲街弑〆低调 提交于 2019-11-27 00:34:41
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? 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 declare most references that I don't intend to change as final . I still can add to ArrayList new elements,

Final variable manipulation in Java

时光怂恿深爱的人放手 提交于 2019-11-27 00:14:03
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? 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 fields of the object it refers to can still be changed, if the class allows it. For example: final

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

半城伤御伤魂 提交于 2019-11-26 23:52:11
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 val privateVal = 0 def testPrivateVal = privateVal private final val privateFinalVal = 1 def