final

Why is there no Constant feature in Java?

风流意气都作罢 提交于 2019-11-26 23:48:04
I was trying to identify the reason behind constants in Java I have learned that Java allows us to declare constants by using final keyword. My question is why didn't Java introduce a Constant ( const ) feature. Since many people say it has come from C++, in C++ we have const keyword. Please share your thoughts. Every time I go from heavy C++ coding to Java, it takes me a little while to adapt to the lack of const-correctness in Java. This usage of const in C++ is much different than just declaring constant variables, if you didn't know. Essentially, it ensures that an object is immutable when

Why are all fields in an interface implicitly static and final?

别说谁变了你拦得住时间么 提交于 2019-11-26 23:31:23
I am just trying to understand why all fields defined in an Interface are implicitly static and final . The idea of keeping fields static makes sense to me as you can't have objects of an interface but why they are final (implicitly)? Any one knows why Java designers went with making the fields in an interface static and final ? Adriaan Koster An interface can't have behavior or state because it is intended to specify only an interaction contract, no implementation details. No behavior is enforced by not allowing method/constructor bodies or static/instance initializing blocks. No state is

Scala final vs val for concurrency visibility

邮差的信 提交于 2019-11-26 22:45:27
问题 In Java, when using an object across multiple threads (and in general), it is good practice to make fields final. For example, public class ShareMe { private final MyObject obj; public ShareMe(MyObject obj) { this.obj = obj; } } In this case, the visibility of obj will be consistent across multiple threads (let's assume obj has all final fields as well) since it is safely constructed using the final keyword. In scala, it doesn't appear val compiles down to a final reference, but rather val is

Changing private final fields via reflection

╄→гoц情女王★ 提交于 2019-11-26 22:07:51
class WithPrivateFinalField { private final String s = "I’m totally safe"; public String toString() { return "s = " + s; } } WithPrivateFinalField pf = new WithPrivateFinalField(); System.out.println(pf); Field f = pf.getClass().getDeclaredField("s"); f.setAccessible(true); System.out.println("f.get(pf): " + f.get(pf)); f.set(pf, "No, you’re not!"); System.out.println(pf); System.out.println(f.get(pf)); Output: s = I’m totally safe f.get(pf): I’m totally safe s = I’m totally safe No, you’re not! Why does it work by this way, can you please explain? The first print tells us that the private "s"

Getting value of public static final field/property of a class in Java via reflection

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 22:03:12
Say I have a class: public class R { public static final int _1st = 0x334455; } How can I get the value of the "_1st" via reflection? First retrieve the field property of the class, then you can retrieve the value. If you know the type you can use one of the get methods with null (for static fields only, in fact with a static field the argument passed to the get method is ignored entirely). Otherwise you can use getType and write an appropriate switch as below: Field f = R.class.getField("_1st"); Class<?> t = f.getType(); if(t == int.class){ System.out.println(f.getInt(null)); }else if(t ==

How does java serialization deserialize final fields when no default constructor specified?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 20:51:01
问题 I have an class defining an immutable value type that I now need to serialize. The immutability comes from the final fields which are set in the constructor. I've tried serializing, and it works (surprisingly?) - but I've no idea how. Here's an example of the class public class MyValueType implements Serializable { private final int value; private transient int derivedValue; public MyValueType(int value) { this.value = value; this.derivedValue = derivedValue(value); } // getters etc... }

Java - Can final variables be initialized in static initialization block?

笑着哭i 提交于 2019-11-26 19:41:04
问题 Based on my understanding of the Java language, static variables can be initialized in static initialization block . However, when I try to implement this in practice ( static variables that are final too), I get the error shown in the screenshot below: 回答1: Yes of course: static final variables can be initialized in a static block but .... you have implicit GOTOs in that example ( try/catch is essentially a 'GOTO catch if something bad happens' ). If an exception is thrown your final

Cannot reference “X” before supertype constructor has been called, where x is a final variable

馋奶兔 提交于 2019-11-26 19:05:02
问题 Consider the following Java class declaration: public class Test { private final int defaultValue = 10; private int var; public Test() { this(defaultValue); // <-- Compiler error: cannot reference defaultValue before supertype constructor has been called. } public Test(int i) { var = i; } } The code will not compile, with the compiler complaining about the line I've highlighted above. Why is this error happening and what's the best workaround? 回答1: The reason why the code would not initially

in what order are static blocks and static variables in a class executed? [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-11-26 18:54:00
Possible Duplicate: Java static class initialization Why is the string variable updated in the initialization block and not the integer(even though the block is written first) class NewClass { static { System.out.println(NewClass.string+" "+NewClass.integer); } final static String string="static"; final static Integer integer=1; public static void main(String [] args)//throws Exception { } } My output is static null P.S:Also noticed that string variable initialization happens before the block only when i insert the final modifier. why is that?why not for integer as well?I have declared it as

Difference between final static and static final

烂漫一生 提交于 2019-11-26 18:48:48
问题 I found a code where it declared code like private final static String API_RTN_SUCCESS = "0"; private final static String API_RTN_ERROR = "1"; public static final String SHARED_PREFERENCE_CONFIG = "shared_preference_config"; public static final String STARTUP_SETTING_KEY = "startup_setting"; What is the difference between them or are they same? Or does it differ for private or public ? 回答1: No difference at all. According to 8.3.1 - Classes - Field Modifiers of the Java Language Specification