final

Java and the use of the Final keyword

半世苍凉 提交于 2020-01-12 09:16:13
问题 I'm new to Java having programmed in Delphi and C# for some time,. My question relates to the usage of the "final" keyword on a variable that holds an instantiated class when the variable declaration and instantiation both happen within the scope of the same method. e.g. private String getDeviceID() { //get the android device id final TelephonyManager tm = (TelephonyManager)GetBaseContext().getSystemService(Context.TELEPHONY_SERVICE); final String deviceID = tm.getDeviceId(); // log debug

Performance differences between static and non-static final primitive fields in Java

跟風遠走 提交于 2020-01-11 05:04:08
问题 I recently ran across a class that had the following field declared: private final int period = 1000; In this particular case, the author had intended for it to also be static and since the value couldn't be altered at any point, there was no real functional reason not to declare it static, but it got me wondering how Java treats final vs. final static primitives. In particular: 1) How are final static primitives stored? Are they simply compiled directly into the expressions in which they're

Performance differences between static and non-static final primitive fields in Java

南笙酒味 提交于 2020-01-11 05:03:32
问题 I recently ran across a class that had the following field declared: private final int period = 1000; In this particular case, the author had intended for it to also be static and since the value couldn't be altered at any point, there was no real functional reason not to declare it static, but it got me wondering how Java treats final vs. final static primitives. In particular: 1) How are final static primitives stored? Are they simply compiled directly into the expressions in which they're

10.其他

依然范特西╮ 提交于 2020-01-10 22:49:26
一、 String相关 二、final: 把对象声明成final仅仅保证了它不会被重新赋上另外一个值,但仍然可以通过此引用来修改引用对象的属性(final User user,可以修改user.name的值)。 来源: CSDN 作者: 放羊的大飞 链接: https://blog.csdn.net/qq_39082976/article/details/103930180

JTextField wants to be a final when i try to use .getText()

孤街醉人 提交于 2020-01-05 07:23:13
问题 I'm working on this multiplayer game for school and I'm currently trying to take a username from a JTextField. Here's the code in question: JTextField textField = new JTextField(); new Statistics(textField.getText()); Statistics takes a string, but when I try this, eclipse is telling me that I need to cast the JTextField as a final. The code works, as Statistics isn't doing anything at the moment, but I don't think this should be happening, and will break my code later on when I do start

IntelliJ apply inspection fix throughout entire file

雨燕双飞 提交于 2020-01-04 05:45:16
问题 In IntelliJ, I have the inspection that checks for variables that can be made final turned on so that IntelliJ will highlight those variables and let me quickly add the final keyword with Alt + Enter . When I open up someone else's code who does not typically use the final keyword, I would like a quick way to add final to all variables that can be throughout the entire file, or even the entire project, if possible. Is there any shortcut or some other way to do this easily in IntelliJ? 回答1:

How to avoid synchronization on a non-final field?

大兔子大兔子 提交于 2020-01-04 05:34:07
问题 If we have 2 classes that operate on the same object under different threads and we want to avoid race conditions, we'll have to use synchronized blocks with the same monitor like in the example below: class A { private DataObject mData; // will be used as monitor // thread 3 public setObject(DataObject object) { mData = object; } // thread 1 void operateOnData() { synchronized(mData) { mData.doSomething(); ..... mData.doSomethingElse(); } } } class B { private DataObject mData; // will be

Proper way to declare and set a private final member variable from the constructor in Java?

安稳与你 提交于 2020-01-04 05:33:14
问题 There are different ways to set a member variable from the constructor. I am actually debating how to properly set a final member variable, specifically a map which is loaded with entries by a helper class. public class Base { private final Map<String, Command> availableCommands; public Base() { availableCommands = Helper.loadCommands(); } } In the above example the helper class looks like this: public class Helper { public static Map<String, Command> loadCommands() { Map<String, Command>

Declaring parameter of a function as final: why and when is it needed?

六月ゝ 毕业季﹏ 提交于 2020-01-03 15:56:37
问题 Going through a tutorial for android (related to multithreading, loopers and handlers), i came across this: public synchronized void enqueueDownload(final DownloadTask task) My questions are: When and why is it needed to declare a parameter of a function as final? Is it specific to Java or does something similar exist in other languages such as C/C++? 回答1: In Java this is usually so that you can access the parameter within an anonymous inner class - which is often used in Android for event

Why can't I declare and initialize static variable in inner class? [duplicate]

北慕城南 提交于 2020-01-03 02:40:11
问题 This question already has answers here : Why does Java prohibit static fields in inner classes? (8 answers) Closed 3 years ago . class A is the outer class. Class B is an inner class of A, and class C is an inner class of B. All three classes declare and initialize one static final variable. This is the object in class A: static final Object x = new Object(); class B: static final Object x1 = new Object(); class C: static final Object x2 = new Object(); The problem is that the variable in