final

Are final fields really useful regarding thread-safety?

六眼飞鱼酱① 提交于 2019-12-20 10:26:06
问题 I have been working on a daily basis with the Java Memory Model for some years now. I think I have a good understanding about the concept of data races and the different ways to avoid them (e.g, synchronized blocks, volatile variables, etc). However, there's still something that I don't think I fully understand about the memory model, which is the way that final fields of classes are supposed to be thread safe without any further synchronization. So according to the specification, if an

Why does C# not allow const and static on the same line?

為{幸葍}努か 提交于 2019-12-20 09:49:32
问题 Why does C# not allow const and static on the same line? In Java, you must declare a field as 'static' and 'final' to act as a constant. Why does C# not let you declare const's as final? I make the further distinction that in Java, every interface is public and abstract, whether this is explicitly declared or not. Aren't const's effectively static in nature? WHy does C# balk at this? 回答1: const and static really do mean different things, different storage mechanism, different initialisation.

Static block is not being called

醉酒当歌 提交于 2019-12-20 08:57:17
问题 Who can explain what's going on? public class MagicFinal { public static void main(String[] args) { System.out.println(A.s); } } class A { static { System.out.println("class has been loaded"); } public static final String s = "final"; public static final Integer i = 3; } Console : final What's it? I don't understand why the class has not been loaded, I know classes always load at the first call. Field s is in pool of string, I see that final modifier is magic. If I delete final modifier (

Issue with Java 8 Lambda for effective final while incrementing counts

▼魔方 西西 提交于 2019-12-20 05:52:37
问题 I want to use Java 8 Lambda expression in following scenario but I am getting Local variable fooCount defined in an enclosing scope must be final or effectively final . I understand what the error message says, but I need to calculate percentage here so need to increment fooCount and barCount then calculate percentage. So what's the way to achieve it: // key is a String with values like "FOO;SomethinElse" and value is Long final Map<String, Long> map = null; .... private int

error: Illegal modifier for parameter

风格不统一 提交于 2019-12-20 04:54:25
问题 I was using these lines of code... public static final int WIDTH = 240; public static final int HEIGHT = 350; public static final int SCALE = 2; but I always got these errors ... Illegal modifier for parameter WIDTH; only final is permitted Illegal modifier for parameter HEIGHT; only final is permitted Illegal modifier for parameter SCALE; only final is permitted 回答1: I expect that you are trying to use public and static on local variable declarations or method parameter declarations. Locals

Final

旧城冷巷雨未停 提交于 2019-12-20 02:36:11
final关键字 一.Final 修饰成员变量的注意事项 final修饰成员变量,该成员变量必须在创建对象之前进行赋值,否则编译失败 final修饰成员变量,固定的不是成员变量拥有的默认值,如果固定的是默认值,那么将导致被final修饰的成员变量的值永远无法修改,只能是默认值,这也不符合语法规则 成员变量的赋值有三种实现方式: 定义成员变量的时候手动赋值 利用构造器对成员变量进行赋值 利用set函数进行赋值(也即利用一般的方法进行赋值) 4.被final修饰的成员变量,只能拥有3中所描述的赋值方法的1,2。 3为什么不行? 解释:如1所描述,被final修饰的成员变量必须在对象创建之前进行赋值,如果方法3可以,那么我们知道对象创建后,才能调用方法3,也就是说成员变量利用方法3进行赋值,会导致成员变量的赋值发生在对象创建之后 5.为什么被Final修饰的成员变量必须在对象创建之前进行赋值? 理解: 被final关键字修饰的东西有一个特点,那就是一旦被修饰,那么它的值也就终生不变,可见final关键字起到了固定的作用,既然起到固定那么,你就要提前告诉final固定的是谁,如果允许被final修饰的成员变量赋值发生在对象创建之后,那么对象创建完成后final固定的值还是未可知的 6.final修饰成员变量和final修饰局部变量的区别与联系: 1

Local variable needs to be declared final

ぐ巨炮叔叔 提交于 2019-12-20 01:14:19
问题 I'm receiving the error "local variable box is accessed from within inner class; needs to be declared final". That seems alright, but I don't really think it's the best solution, so I was hoping maybe someone else can help me out. Here is my code: public void showPublisherBox(JComboBox box) { if (publisherBox == null) { publisherBox = new AddPublisherForm(); publisherBox.setLocationRelativeTo(this); } publisherBox.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent

Local variable needs to be declared final

别来无恙 提交于 2019-12-20 01:13:58
问题 I'm receiving the error "local variable box is accessed from within inner class; needs to be declared final". That seems alright, but I don't really think it's the best solution, so I was hoping maybe someone else can help me out. Here is my code: public void showPublisherBox(JComboBox box) { if (publisherBox == null) { publisherBox = new AddPublisherForm(); publisherBox.setLocationRelativeTo(this); } publisherBox.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent

Scope of final local variable in java

对着背影说爱祢 提交于 2019-12-19 17:47:23
问题 Method-Local inner class cannot access local variables because the instance of the method-local inner class may still alive after the method is over. But local variables will vanish once the local method is over. I learned that method-local inner class can access final local variable, does this mean final local variable still alive after the method is over? 回答1: Sort of. Java anonymous inner classes act like "closures," that is, they "close" around the current local state. However, Java only

variable might already have been assigned when it cannot be assigned

南楼画角 提交于 2019-12-19 16:47:58
问题 research this code: public class TestFinalAndCatch { private final int i; TestFinalAndCatch(String[] args) { try { i = method1(); } catch (IOException ex) { i = 0; // error: variable i might already have been assigned } } static int method1() throws IOException { return 1; } } compiler says that java: variable i might already have been assigned But for me it is looks like impossible situation. 回答1: i is final, so it can only be assigned once. The compiler is probably not smart enough to