final

Why can't a Java enum be final?

雨燕双飞 提交于 2019-11-27 14:31:01
问题 public interface Proposal { public static final enum STATUS { NEW , START , CONTINUE , SENTTOCLIENT }; } Java does not allow an enum to be final inside an interface, but by default every data member inside an interface is public static final . Can anybody clarify this? 回答1: An enum can't be final, because the compiler will generate subclasses for each enum entry that the programmer has explicitly defined an implementation for. Moreover, an enum where no instances have their own class body is

Initialize final variable before constructor in Java

前提是你 提交于 2019-11-27 14:30:32
Is there a solution to use a final variable in a Java constructor? The problem is that if I initialize a final field like: private final String name = "a name"; then I cannot use it in the constructor. Java first runs the constructor and then the fields. Is there a solution that allows me to access the final field in the constructor? I do not really understand your question. That public class Test3 { private final String test = "test123"; public Test3() { System.out.println("Test = "+test); } public static void main(String[] args) { Test3 t = new Test3(); } } executes as follows: $ javac Test3

java之final关键字

一曲冷凌霜 提交于 2019-11-27 13:00:15
final关键字特点 final修饰类:final修饰的类不能被继承 final修饰变量:final修饰的变量不能被二次修改,也称为常量 final修饰方法:final修饰的方法不能被重写 来源: https://blog.csdn.net/dreamchaser1997/article/details/99678250

final variable in methods in Java [duplicate]

ぐ巨炮叔叔 提交于 2019-11-27 12:47:35
问题 Possible Duplicate: Why would one mark local variables and method parameters as “final” in Java? I was checking some Java code, I am not good at java at least have some knowledge what final does such as sealed classes, readonly fields and non-overridable methods but this looks weird to me, declaring a variable final in methods: private static void queryGoogleBooks(JsonFactory jsonFactory, String query) throws Exception { // Set up Books client. final Books books = Books.builder(new

How to handle a static final field initializer that throws checked exception

笑着哭i 提交于 2019-11-27 12:36:03
I am facing a use case where I would like to declare a static final field with an initializer statement that is declared to throw a checked exception. Typically, it'd look like this: public static final ObjectName OBJECT_NAME = new ObjectName("foo:type=bar"); The issue I have here is that the ObjectName constructor may throw various checked exceptions, which I don't care about (because I'd know my name is valid, and it's allright if it miserably crashes in case it's not). The java compiler won't let me just ignore this (as it's a checked exception), and I would prefer not to resort to: public

About reference to object before object's constructor is finished

我的未来我决定 提交于 2019-11-27 11:58:46
问题 Every one of you know about this feature of JMM , that sometimes reference to object could receive value before constructor of this object is finished. In JLS7, p. 17.5 final Field Semantics we can also read: The usage model for final fields is a simple one: Set the final fields for an object in that object's constructor; and do not write a reference to the object being constructed in a place where another thread can see it before the object's constructor is finished . If this is followed,

Quick Java question about private static final keywords for fields

末鹿安然 提交于 2019-11-27 11:42:31
问题 I'm declaring a field: private static final String filename = "filename.txt"; First, does the order of private static final matter? If not, is there a standard accepted sequence or convention? Second, the filename in my application is fixed. Is this the best was to store its value? 回答1: I use Checkstyle with Eclipse, which results in a warning if the declaration is in a different order to the one you've specified, citing the Java Language Specification (JLS). For example, private final static

Initialize a static final field in the constructor

五迷三道 提交于 2019-11-27 10:54:36
public class A { private static final int x; public A() { x = 5; } } final means the variable can only be assigned once (in the constructor). static means it's a class instance. I can't see why this is prohibited. Where do those keywords interfere with each other? A constructor will be called each time an instance of the class is created. Thus, the above code means that the value of x will be re-initialized each time an instance is created. But because the variable is declared final (and static), you can only do this class A { private static final int x; static { x = 5; } } But, if you remove

Are final static variables thread safe in Java?

三世轮回 提交于 2019-11-27 10:51:53
问题 I've read around quite a bit but haven't found a definitive answer. I have a class that looks like this: public class Foo() { private static final HashMap<String, HashMap> sharedData; private final HashMap myRefOfInnerHashMap; static { // time-consuming initialization of sharedData final HashMap<String, String> innerMap = new HashMap<String, String>; innerMap.put... innerMap.put... ...a sharedData.put(someKey, java.util.Collections.unmodifiableMap(innerMap)); } public Foo(String key) { this

Double-checked locking without volatile

非 Y 不嫁゛ 提交于 2019-11-27 10:51:11
问题 I read this question about how to do Double-checked locking: // Double-check idiom for lazy initialization of instance fields private volatile FieldType field; FieldType getField() { FieldType result = field; if (result == null) { // First check (no locking) synchronized(this) { result = field; if (result == null) // Second check (with locking) field = result = computeFieldValue(); } } return result; } My aim is to get lazy-loading a field (NOT a singleton) work without the volatile attribute