final

Good reasons to prohibit inheritance in Java?

大城市里の小女人 提交于 2019-12-17 02:40:11
问题 What are good reasons to prohibit inheritance in Java, for example by using final classes or classes using a single, private parameterless constructor? What are good reasons of making a method final? 回答1: Your best reference here is Item 19 of Joshua Bloch's excellent book "Effective Java", called "Design and document for inheritance or else prohibit it". (It's item 17 in the second edition and item 15 in the first edition.) You should really read it, but I'll summarize. The interaction of

Compiler added optimization causes different behavior for “final” methods [closed]

守給你的承諾、 提交于 2019-12-13 16:14:29
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 7 years ago . I had asked a post with the same title. But due to misinterpretation, all the answers were discussing if it's a valid code/behavior or not. However the

java basics about final keyword

若如初见. 提交于 2019-12-13 11:51:18
问题 Can final keyword be used for a method? 回答1: Absolutely! The final keyword can be applied to just about anything, in each case meaning "you don't get to change this anymore." Here's what it means when applied to... a variable : You simply cannot assign the variable a new value (rendering it a constant , of course) a method : You cannot re-implement (i.e., override) this method in a subclass a class : You cannot define a subclass In each case we're simply indicating: once this thing is

MySQL SELECT and ORDER BY [closed]

烂漫一生 提交于 2019-12-13 09:56:11
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . In relation to: MySQL ORDER BY or GROUP BY, I do it more complicated. I have an id_competitor with various scores in different routes. id_competitor score id_route 1 WIN 2 2 WIN 2 3 50+ 2 4 50+ 2 1 70 1 2 70+ 1 3 70 1 4 WIN 1 Here is the table "route" id name 1 semi-final 2 final The result should be in the

J-Unit Test: Make static void method in final class throw exception

不羁岁月 提交于 2019-12-13 06:36:25
问题 I am writing J-Unit Tests for my project and now this problem occured: I am testing a servlet which uses a Utility class (class is final, all methods are static). The used method returns void and can throw an IOException (httpResponse.getWriter). Now I have to force this exception... I have tried and searched a lot, but all the solutions I found did not worked, because there was no combination of final, static, void, throw . Has anyone did this before? EDIT: Here is the code snippet Servlet:

Thread safety for classes with non-final members with no mutator methods

会有一股神秘感。 提交于 2019-12-13 05:19:50
问题 For the following class, what are the implications of making mNumSides final? Is "thread-safety" affected? class Shape { private int mNumSides; public Shape(int numSides) { mNumSides = numSides; } public int getNumSides() { return mNumSides; } } 回答1: Absolutely. final keyword guarantees that all the threads see the same value of mNumSides at all the times. More information on final and its impact on the Memory Model here. Without using final the object might be inconsistently published to

Adding ArrayList items to a user-defined class with final values in Java

我们两清 提交于 2019-12-13 03:58:38
问题 I have a text file with thousands of lines of data like the following: 38.48,88.25 48.20,98.11 100.24,181.39 83.01,97.33 I can separate each "double" value just fine, but I'm having trouble adding each line to my user-defined class. In my main method, I created a list by: List<Pair> data = new ArrayList<Pair>(); Where Pair is defined as: class Pair { private final double first; private final double second; public Pair(double first, double second) { this.first = first; this.second = second; }

Immutable Java class with non-final member

家住魔仙堡 提交于 2019-12-13 03:55:27
问题 I still have some problems grasping the idea of immutability in Java. I understand that it differs from the const -ness in C++ and that a final class that only has final members of classes that are immutable themselves is immutable. E.g. the following class is immutable: public final class A { final String x; final int y; public A(String x, String y) { this.x = x; this.y = y; } } Is there some formal definition besides the guidelines presented here and similar stuff somewhere else? Consider

Java lazy thread safe singleton with implemented with final field

久未见 提交于 2019-12-13 03:34:03
问题 I do not understand why local variable is needed here: public class FinalWrapper<T> { public final T value; public FinalWrapper(T value) { this.value = value; } } public class Foo { private FinalWrapper<Helper> helperWrapper; public Helper getHelper() { FinalWrapper<Helper> tempWrapper = helperWrapper; if (tempWrapper == null) { synchronized(this) { if (helperWrapper == null) { helperWrapper = new FinalWrapper<Helper>(new Helper()); } tempWrapper = helperWrapper; } } return tempWrapper.value;

Static,Final

杀马特。学长 韩版系。学妹 提交于 2019-12-12 19:56:59
final关键字主要用在三个地方:变量、方法、类。 对于一个final变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改;如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一个对象。 当用final修饰一个类时,表明这个类不能被继承。final类中的所有成员方法都会被隐式地指定为final方法。 使用final方法的原因有两个。第一个原因是把方法锁定,以防任何继承类修改它的含义;第二个原因是效率。在早期的Java实现版本中,会将final方法转为内嵌调用。但是如果方法过于庞大,可能看不到内嵌调用带来的任何性能提升(现在的Java版本已经不需要使用final方法进行这些优化了)。类中所有的private方法都隐式地指定为final。 static 关键字 static 关键字主要有以下四种使用场景: 修饰成员变量和成员方法: 被 static 修饰的成员属于类,不属于单个这个类的某个对象,被类中所有对象共享,可以并且建议通过类名调用。被static 声明的成员变量属于静态成员变量,静态变量 存放在 Java 内存区域的方法区。调用格式: 类名.静态变量名 类名.静态方法名() 静态代码块: 静态代码块定义在类中方法外, 静态代码块在非静态代码块之前执行(静态代码块—>非静态代码块—>构造方法)。 该类不管创建多少对象,静态代码块只执行一次. 静态内部类