final

Why private method can not be final as well?

与世无争的帅哥 提交于 2019-11-30 05:36:17
Is it redundant to add private and final to a same method? class SomeClass { //--snip-- private final void doStuff() { // private work here } } If it's private , there's no way anyone can override it, right? Why is it possible to add final keyword if it has no effect? (or am I missing something?) Basically, it's allowed because they didn't feel like it's worthwhile to put a special case prohibiting the private modifier. It's like how you can also declare methods on an interface as public , or nested classes in an interface as static , even though those keywords are implied in interfaces. You

Final variable and synchronized block in java

独自空忆成欢 提交于 2019-11-30 05:19:03
问题 What is a final variable in Java? For example: if I write final int temp; in function what is the meaning of the final keyword? Also, when would I want to use final variable (both as a class variable and as a function variable)? Why must variables in a synchronized block be declared final? 回答1: Basically it just means you can't change the value. For instance variables, you have to assign any final variables once (and only once) in the constructor (or with a variable initializer).

A transient final field used as a lock is null

吃可爱长大的小学妹 提交于 2019-11-30 04:48:29
问题 The following code throws a NullPointerException . import java.io.*; public class NullFinalTest { public static void main(String[] args) throws IOException, ClassNotFoundException { Foo foo = new Foo(); foo.useLock(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); new ObjectOutputStream(buffer).writeObject(foo); foo = (Foo) new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())).readObject(); foo.useLock(); } public static class Foo implements Serializable { private

Passing final variables to anonymous classes

﹥>﹥吖頭↗ 提交于 2019-11-30 04:48:16
In final variable passed to anonymous class via constructor , Jon Skeet mentioned that variables are passed to the anonymous class instance via an auto-generated constructor. Why would I not be able to see the constructor using reflection in that case: public static void main(String... args) throws InterruptedException { final int x = 100; new Thread() { public void run() { System.out.println(x); for (Constructor<?> cons : this.getClass() .getDeclaredConstructors()) { StringBuilder str = new StringBuilder(); str.append("constructor : ").append(cons.getName()) .append("("); for (Class<?> param

Why can final constants in Java be overridden?

流过昼夜 提交于 2019-11-30 02:50:22
Consider the following interface in Java: public interface I { public final String KEY = "a"; } And the following class: public class A implements I { public String KEY = "b"; public String getKey() { return KEY; } } Why is it possible for class A to come along and override interface I's final constant? Try for yourself: A a = new A(); String s = a.getKey(); // returns "b"!!! André Despite the fact that you are shadowing the variable it's quite interesting to know that you can change final fields in java as you can read here : Java 5 - "final" is not final anymore Narve Saetre from Machina

Immutable Value objects and JPA

十年热恋 提交于 2019-11-30 01:11:11
问题 Is there way to map immutable Value objects like email address using JPA? @Immutable @Embeddable public final class EmailAddress { private final String value; public EmailAddress(String value) { this.value = value; } public String getValue() { return value; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; EmailAddress that = (EmailAddress) o; return value.equals(that.value); } @Override public int hashCode() {

final characters in Java [duplicate]

蓝咒 提交于 2019-11-30 00:57:52
问题 This question already has answers here : Why can not I add two bytes and get an int and I can add two final bytes get a byte? (2 answers) Java - char, int conversions (4 answers) Closed 5 years ago . The following segment of code issues a compile-time error. char c = 'c'; char d = c + 5; The error on the second line says, possible loss of precision required: char found: int The error message is based on the NetBeans IDE. When this character c is declared final like as follows. final char c =

Should a java class' final fields always be static?

那年仲夏 提交于 2019-11-30 00:45:51
问题 I could not find any references online about this. But just wanted to know if final fields in a class should always be static or is it just a convention. Based on my understanding of their uses, I feel that it is more of a logical thing to do than something that is imposed by the language. 回答1: Of course not. They must be static if they belong to the class, and not be static if they belong to the instance of the class: public class ImmutablePerson { private static final int MAX_LAST_NAME

implementation safe nullptr

て烟熏妆下的殇ゞ 提交于 2019-11-29 20:58:49
问题 I'd like to keep my code compilable both on legacy C++ (C++ code using "NULL") and new C++11 standard (C++ code using "nullptr") I'm using GCC, but planning to recompile the whole codebase also for VS when I'll finish most important things. Should I expect both GCC and VS will do something like #define NULL nullptr Or Is better I'll do that myself (using of course a different name, where MY_LIB will be replaced by my library suffix)? #ifndef nullptr #define MY_LIB_NULL NULL #else #define MY

'public static final' or 'private static final' with getter?

柔情痞子 提交于 2019-11-29 19:59:03
In Java, it's taught that variables should be kept private to enable better encapsulation, but what about static constants? This: public static final int FOO = 5; Would be equivalent in result to this: private static final int FOO = 5; ... public static getFoo() { return FOO; } But which is better practice? There's one reason to not use a constant directly in your code. Assume FOO may change later on (but still stay constant), say to public static final int FOO = 10; . Shouldn't break anything as long as nobody's stupid enough to hardcode the value directly right? No. The Java compiler will