final

What's the use case of a protected method in a final class in Java?

孤人 提交于 2019-12-01 16:03:40
问题 Consider this code from the official OpenJDK source of java.awt.font.TextLayout : public final class TextLayout { /* ... */ protected void handleJustify(float justificationWidth) { // never called } } What's the use case here and why might it make sense to write code like that in general? 回答1: protected members can still be accessed by code from the same package. My guess is that the class used to be non-final in some earlier (perhaps not even public) version, was then made final, and the

Problems initializing a final variable in Java

ぃ、小莉子 提交于 2019-12-01 13:47:26
问题 I keep running into slight variations of a problem in Java and it's starting to get to me, and I can't really think of a proper way to get around it. I have an object property that is final, but dynamic. That is, I want the value to be constant once assigned, but the value can be different each runtime. So I declare the class level variable at the beginning of the class - say private final FILE_NAME; . Then, in the constructor, I assign it a value - say FILE_NAME = buildFileName(); The

why using plain val in non-final classes

与世无争的帅哥 提交于 2019-12-01 08:44:18
If class is not a final one, it may be extended. There are two possibilities for values: it may be overridden and should be lazy for it, it may not be overridden and should be final. If val is final - you may assume that all computations over it would work through class hierarchy. If val may be overriden you should declare it lazy for not becoming broken after extending. You may leave val plain and this gives no guaranties it would be extended in right way. What use cases imply using plain values? Example of class initialization failure without lazy values abstract class A { lazy val x1 :

Why is the “this” keyword final in Java? [duplicate]

我们两清 提交于 2019-12-01 07:40:55
问题 This question already has answers here : Why is assignment to 'this' not allowed in java? (7 answers) Closed 5 years ago . It seems a thing that almost no one has realized, but the "this reference" in Java is final. In a normal programming day I thought I could redefine the entire instance by redefining the this reference inside the own class: public void method() { this = new MyObject("arg1", arg2, arg3); //it throws a compile error } Why the this reference is final in Java? 回答1: The problem

why using plain val in non-final classes

ε祈祈猫儿з 提交于 2019-12-01 06:28:08
问题 If class is not a final one, it may be extended. There are two possibilities for values: it may be overridden and should be lazy for it, it may not be overridden and should be final. If val is final - you may assume that all computations over it would work through class hierarchy. If val may be overriden you should declare it lazy for not becoming broken after extending. You may leave val plain and this gives no guaranties it would be extended in right way. What use cases imply using plain

Do javac or Hotspot automatically add 'final' as an optimisation of invariant variables?

断了今生、忘了曾经 提交于 2019-12-01 06:11:05
The consensus seems to be that there is a performance benefit to marking member variables as final because they never need reloading from main memory. My question is, do javac or Hotspot automatically do this for me when it's obvious the variable cannot change. eg will javac make 'x' final in this class below... public class MyClass { private String x; MyClass(String x) { this.x = x; } public String getX() { return x; } } On a secondary point, has anyone produced empirical evidence that marking members as final makes code run faster? Any benefit is surely negligible in any application making

Why instance variable to be final?

↘锁芯ラ 提交于 2019-12-01 05:50:29
I read this question about immutable objects and was left with a question regarding immutable objects and final field: Why do we need instance variable in immutable class to be final? For example, consider this immutable class: public final class Immutable { private final int someVal; public Immutable(int someVal) { this.someVal= someVal; } public int getVal() { return val; } } If in the above code there is no set methods and the instance variable is set only within the constructor, why is it required that the instance variable is declared final? There is no requirement as such to make the

Can You Write an Interface that Can Not be implemented?

≯℡__Kan透↙ 提交于 2019-12-01 05:10:40
问题 This is related to final interface in java. Among the discussion there was that the concept of final in relation to interfaces is ambiguous. Would a final interface mean that it can not have subinterfaces? Would it mean that it can not have implementations? This question is the first: can you write an final interface such that the compiler will prevent you from implementing it? 回答1: As I will show, it is possible to implement the interface above using a proxy. The more meaningful question is,

java :Why the Local variable should be declared final [duplicate]

↘锁芯ラ 提交于 2019-12-01 04:27:47
Possible Duplicate: Is there any performance reason to declare method parameters final in Java? Why would one mark local variables and method parameters as “final” in Java? I am using PMD to see the code violations. Inside a webService Method, I have this below code public ServiceRequest getData() { Status status = new Status(); // code } What PMD is suggesting me is that, this local variable status could be declared as final. My question is, making it final would result in any performance improvements or if not what benefits the code could get? Nick Garvey Taken from the following article:

The blank final field INITIAL may not have been initialized

大憨熊 提交于 2019-12-01 03:38:07
I'm programming in Java. I have added comments to every method to explain what they're supposed to do (according to the assignment). I have added what I know to the stub of Password.java (which I created after researching a javadoc provided by the school). My question is not about several functions, I know there are mistakes in testWord and setWord, but I'll handle that myself. My question is about this line: public static final java.lang.String INITIAL; This line is provided by the school, so I gotta assume that it is correct, I cant find any documentation anywhere about the constant field