final

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

北城余情 提交于 2019-11-26 08:09:53
问题 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? 回答1: 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

Why can final object be modified?

坚强是说给别人听的谎言 提交于 2019-11-26 06:18:50
问题 I came across the following code in a code base I am working on: public final class ConfigurationService { private static final ConfigurationService INSTANCE = new ConfigurationService(); private List providers; private ConfigurationService() { providers = new ArrayList(); } public static void addProvider(ConfigurationProvider provider) { INSTANCE.providers.add(provider); } ... INSTANCE is declared as final . Why can objects be added to INSTANCE ? Shouldn\'t that invalidate the use of final.

Should a “static final Logger” be declared in UPPER-CASE?

有些话、适合烂在心里 提交于 2019-11-26 06:10:30
问题 In Java, static final variables are constants and the convention is that they should be in upper-case. However, I have seen that most people declare loggers in lower-case which comes up as a violation in PMD. e.g: private static final Logger logger = Logger.getLogger(MyClass.class); Just search googleor SO for \"static final logger\" and you will see this for yourself. Should we be using LOGGER instead? 回答1: The logger reference is not a constant, but a final reference, and should NOT be in

Is there any performance reason to declare method parameters final in Java?

守給你的承諾、 提交于 2019-11-26 05:58:10
问题 Is there any performance reason to declare method parameters final in Java? As in: public void foo(int bar) { ... } Versus: public void foo(final int bar) { ... } Assuming that bar is only read and never modified in foo() . 回答1: The final keyword does not appear in the class file for local variables and parameters, thus it cannot impact the runtime performance. It's only use is to clarify the coders intent that the variable not be changed (which many consider dubious reason for its usage),

Checking a member exists, possibly in a base class, C++11 version

别说谁变了你拦得住时间么 提交于 2019-11-26 05:23:08
问题 In https://stackoverflow.com/a/1967183/134841, a solution is provided for statically checking whether a member exists, possibly in a subclass of a type: template <typename Type> class has_resize_method { class yes { char m;}; class no { yes m[2];}; struct BaseMixin { void resize(int){} }; struct Base : public Type, public BaseMixin {}; template <typename T, T t> class Helper{}; template <typename U> static no deduce(U*, Helper<void (BaseMixin::*)(), &U::foo>* = 0); static yes deduce(...);

Why isn&#39;t a qualified static final variable allowed in a static initialization block?

佐手、 提交于 2019-11-26 04:45:47
问题 Case 1 class Program { static final int var; static { Program.var = 8; // Compilation error } public static void main(String[] args) { int i; i = Program.var; System.out.println(Program.var); } } Case 2 class Program { static final int var; static { var = 8; //OK } public static void main(String[] args) { System.out.println(Program.var); } } Why does Case 1 cause a compilation error? 回答1: The JLS holds the answer (note the bold statement): Similarly, every blank final variable must be

Must all properties of an immutable object be final?

南楼画角 提交于 2019-11-26 04:20:14
问题 Must immutable objects have all properties be final ? According to me not. But I don\'t know, whether I am right. 回答1: The main difference between an immutable object (all properties final) and an effectively immutable object (properties aren't final but can't be changed) is safe publication. You can safely publish an immutable object in a multi threaded context without having to worry about adding synchronization, thanks to the guarantees provided by the Java Memory Model for final fields:

Compile-time constants and variables

南笙酒味 提交于 2019-11-26 04:11:23
问题 The Java language documentation says: If a primitive type or a string is defined as a constant and the value is known at compile time, the compiler replaces the constant name everywhere in the code with its value. This is called a compile-time constant. My understanding is if we have a piece of code: private final int x = 10; Then, the compiler will replace every occurrence of x in the code with literal 10 . But suppose the constant is initialized at run-time: private final int x = getX(); //

Why would one declare an immutable class final in Java?

試著忘記壹切 提交于 2019-11-26 03:51:09
问题 I read that to make a class immutable in Java, we should do the following, Do not provide any setters Mark all fields as private Make the class final Why is step 3 required? Why should I mark the class final ? 回答1: If you don't mark the class final , it might be possible for me to suddenly make your seemingly immutable class actually mutable. For example, consider this code: public class Immutable { private final int value; public Immutable(int value) { this.value = value; } public int

java: “final” System.out, System.in and System.err?

南楼画角 提交于 2019-11-26 03:39:39
问题 System.out is declared as public static final PrintStream out . But you can call System.setOut() to reassign it. Huh? How is this possible if it\'s final ? (same point applies to System.in and System.err ) And more importantly, if you can mutate the public static final fields, what does this mean as far as the guarantees (if any) that final gives you? (I never realized nor expected System.in/out/err behaved as final variables) 回答1: JLS 17.5.4 Write Protected Fields: Normally, final static