final

when exactly are we supposed to use “public static final String”?

天大地大妈咪最大 提交于 2019-11-28 15:58:33
I have seen much code where people write public static final String mystring = ... and then just use a value. Why do they have to do that? Why do they have to initialize the value as final prior to using it? UPDATE Ok, thanks all for all your answers, I understand the meaning of those key (public static final). What I dont understand is why people use that even if the constant will be used only in one place and only in the same class. why declaring it? why dont we just use the variable? pb2q final indicates that the value of the variable won't change - in other words, a variable whose value

In Java, what purpose do the keywords `final`, `finally` and `finalize` fulfil?

三世轮回 提交于 2019-11-28 15:08:03
In Java, what purpose do the keywords final , finally and finalize fulfil? final final can be used to mark a variable "unchangeable" private final String name = "foo"; //the reference name can never change final can also make a method not "overrideable" public final String toString() { return "NULL"; } final can also make a class not "inheritable". i.e. the class can not be subclassed. public final class finalClass {...} public class classNotAllowed extends finalClass {...} // Not allowed finally finally is used in a try/catch statement to execute code "always" lock.lock(); try { //do stuff }

inside JButton's actionperformed final variables required? [duplicate]

谁说我不能喝 提交于 2019-11-28 13:01:20
This question already has an answer here: Cannot refer to a non-final variable inside an inner class defined in a different method 20 answers So i have a JList and i am trying to use it inside of a JButton s actionPerformed method and it is asking me to make the JList final why is that, below is a code snippet public SomeClass() { btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { list.clearSelection(); }}); } I don't actually have a problem making it final, I just am not sure why i would need to. To answer your question, you need to understand

Java Lambda Expression with Non-final Function Paramter

泄露秘密 提交于 2019-11-28 11:13:41
问题 I am trying to simply time a function by using the Runnable interface to wrap around whichever function I need. private static double time(Runnable runnable) { // returns how long runnable took long startTime = System.nanoTime(); runnable.run(); return (System.nanoTime() - startTime) / 1000000000.0; } then I could simply do the following: double durationOfFunctionA = time(Driver::functionA); // functionA belongs in class Driver however, if I have a function that takes a parameter, it must be

Can a final variable be initialized when an object is created?

泄露秘密 提交于 2019-11-28 10:12:59
how it can be possible that we can initialize the final variable of the class at the creation time of the object ? Anybody can explain it how is it possible ? ... You must initialize a final variable once and only once. There are three ways to do that for an instance variable: in the constructor in an instance initialization block. when you declare it Here is an example of all three: public class X { private final int a; private final int b; private final int c = 10; { b = 20; } public X(final int val) { a = val; } } In each case the code is run once when you call new X(...) and there is no

JUC AQS ReentrantLock源码分析

一曲冷凌霜 提交于 2019-11-28 10:12:40
  为了了解java并发包下的 ReentrantLock 可重入锁 和 AbstractQueuedSynchronizer 抽象队列同步器,我自己创建了这两个类,改一下类名,把他们的代码贴过来,再删除英文注释,一步步的分析,再贴上自己的中文理解。然后我发现 BacAQS只有940行,BacReentrantLock 只有230行,然后攻克她。Bac 是我起的英文名,高大上的寓意是 大数据BigData ,Ai人工智能,CloudComputing云计算。读音:巴西。不是喜欢足球,是自己胖得像球。。。  直接上代码,后续会慢慢解读,补充中文注释。你也可以按这个方法,一起学习哦。可重入锁: package www.itbac.com; import java.util.Collection; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; //可重入锁 public class BacReentrantLock implements Lock, java.io.Serializable { private static final long serialVersionUID =

Why is a `val` inside an `object` not automatically final?

谁说胖子不能爱 提交于 2019-11-28 09:36:55
What is the reason for val s not (?) being automatically final in singleton objects? E.g. object NonFinal { val a = 0 val b = 1 def test(i: Int) = (i: @annotation.switch) match { case `a` => true case `b` => false } } results in: <console>:12: error: could not emit switch for @switch annotated match def test(i: Int) = (i: @annotation.switch) match { ^ Whereas object Final { final val a = 0 final val b = 1 def test(i: Int) = (i: @annotation.switch) match { case `a` => true case `b` => false } } Compiles without warnings, so presumably generates the faster pattern matching table. Having to add

enum不能被继承

[亡魂溺海] 提交于 2019-11-28 08:18:50
1、枚举类介绍 如果一个类的实例是有限且确定的,那么可以使用枚举类。比如:季节类,只有春夏秋冬四个实例。 枚举类使用enum进行创建,其实例必须从”第一行“开始显示写出。 enum Season{    SPRING,SUMMER,FALL,WINTER;//默认public static final,这都是Season类的对象。}    特点:     1、枚举类的对象默认都是public static final     2、枚举类的构造器都是private,所以无法在外部创建其实例,这也决定了枚举类实例的个数的确定性(写了几个就是几个)。     3、enum类不可被继承。                   4、enum类默认extends java.lang.Enum,所以无法再继承其他类 2、遇到的问题 enum类默认被final修饰的情况下,是无法有子类的。但是在《疯狂Java讲义》中是这么说的: 那么,问题来了: enum Season{ // SPRING,SUMMER,FALL,WINTER;//默认public static final,这都是Season类的对象。 SPRING{ public void show(){ System.out.println("I'm Spring"); } },SUMMER,FALL,WINTER; private

Why final instance class variable in Java?

為{幸葍}努か 提交于 2019-11-28 07:37:11
If instance variable is set final its value can not be changed like public class Final { private final int b; Final(int b) { this.b = b; } int getFinal() { return b = 8; // COMPILE TIME ERROR } } Somewhere in code I have seen instance class variable HashMap declared as final private final Map<String, Object> cacheMap = new HashMap<String, Object>(); I could not understand why it is declared so? Normally in which case it is declared. Does it mean if once I put in hash map then I could not change its value? Edit: If cacheMap which is declared as final is passed as parameter to another class then

Why it says that “Cannot refer to a non-final variable i inside an inner class defined in a different method”? [duplicate]

谁说胖子不能爱 提交于 2019-11-28 06:33:21
This question already has an answer here: Cannot refer to a non-final variable inside an inner class defined in a different method 20 answers I have button click listener and in onCreate() method I have a local variable like onCreate() { super.onCreate(); int i = 10; Button button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { i++; } }); Why java asks for to make me final ? When the onCreate() method returns, your local variable will be cleaned up from the stack, so they won't exist anymore. But the anonymous