a只能在这里决赛.为什么?如何a在onClick()不将其保留为私有成员的情况下重新分配方法?
private void f(Button b, final int a){
b.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
int b = a*5;
}
});
}
Run Code Online (Sandbox Code Playgroud)如何5 * a点击它返回?我的意思是,
private void f(Button b, final int a){
b.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
int b = a*5;
return b; // but return type is void
}
});
}
Run Code Online (Sandbox Code Playgroud)我在Java 8中玩lambdas,我遇到了警告local variables referenced from a lambda expression must be final or effectively final.我知道当我在匿名类中使用变量时,它们必须在外部类中是最终的,但仍然 - 最终和有效最终之间有什么区别?
编辑:我需要更改几个变量的值,因为它们通过计时器运行几次.我需要通过计时器每次迭代不断更新值.我无法将值设置为final,因为这会阻止我更新值,但是我收到了我在下面的初始问题中描述的错误:
我以前写过以下内容:
我收到错误"不能引用在不同方法中定义的内部类中的非final变量".
这种情况发生在双重调用价格和价格调用priceObject上.你知道我为什么会遇到这个问题.我不明白为什么我需要最后的声明.此外,如果你能看到我想要做的是什么,我该怎么做才能解决这个问题.
public static void main(String args[]) {
int period = 2000;
int delay = 2000;
double lastPrice = 0;
Price priceObject = new Price();
double price = 0;
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
price = priceObject.getNextPrice(lastPrice);
System.out.println();
lastPrice = price;
}
}, delay, period);
}
Run Code Online (Sandbox Code Playgroud) class Outer{
private String x = "instance variable";
void doStuff(){
String z = "local variable";
class Inner{
public void seeOuter(){
System.out.println("Outer x is : "+ x);
System.out.println("Local variable z is : " + z); //won't compile
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
将局部变量z标记为final可解决问题:
final String z = "local variable"; //Now inner object can use it.
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下发生了什么吗?
我确切地知道为什么在我尝试访问非最终局部变量的情况下它无法编译.
即使方法完成且局部变量超出范围,创建局部变量final也能保持活着吗?
最终的局部变量是否存储在堆而不是堆栈中?
我知道你可以安全地发布一个非线程安全的对象,方法是将一个final或一个volatile字段的引用写入,稍后将被其他一个线程读取,前提是在发布时,创建该对象的线程会丢弃对它的引用,以便它不能再干扰或不安全地观察对象在另一个线程中的使用.
但在这个例子中,没有显式final字段,只有final局部变量. 如果调用者丢弃其引用unsafe,这是安全的出版物吗?
void publish(final Unsafe unsafe) {
mExecutor.execute(new Runnable() {
public void run() {
// do something with unsafe
}
}
}
Run Code Online (Sandbox Code Playgroud)
我发现了一些Q&As,就像这个一样,暗示final局部变量被隐式"复制"到匿名类中.这是否意味着上面的例子等同于此?
void publish(final Unsafe unsafe) {
mExecutor.execute(new Runnable() {
final Unsafe mUnsafe = unsafe;
public void run() {
// do something with mUnsafe
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑以澄清:
Unsafe 可能是什么,但说它是这样的:
public class Unsafe {
public int x;
}
Run Code Online (Sandbox Code Playgroud)
并且mExecutor是满足合同的任何东西Executor.
可能重复:
不能引用在不同方法中定义的内部类中的非final变量
为什么内部类需要"final"外部实例变量[Java]?
class MyOuter {
private String x = "Outer";
void doStuff(){
final String z = "local variable";
class MyInner {
public void seeOuter(){
System.out.println("Outer x is" + x);
System.out.println("Local variable z is" + z); // does
// not compile if final keyword from String z is removed
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常.我想知道如果从String z中删除final关键字,编译器为什么会出错.最终的关键字制作有什么区别?
在SO和谷歌寻找这个问题的答案,但找不到任何答案.
我有以下代码:
MyClass variable = new MyClass();
Button b = new Button();
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println("You clicked the button");
variable.doSomething();
}
});
Run Code Online (Sandbox Code Playgroud)
编译器返回:
从内部类中访问局部变量变量; 需要宣布最终
什么variable必须是最终的技术原因是什么?