private void pushButtonActionPerformed(java.awt.event.ActionEvent evt)
{
final int c=0;
final JDialog d=new JDialog();
JLabel l=new JLabel("Enter the Element :");
JButton but1=new JButton("OK");
JButton but2=new JButton("Cancel");
final JTextField f=new JTextField(10);
JPanel panel = new JPanel();
but1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
c=Integer.parseInt(f.getText());
d.setVisible(false);
d.dispose( );
}
});
but2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
d.setVisible(false);
d.dispose( );
}
});
}
Run Code Online (Sandbox Code Playgroud)
我正在使用netbeans 7.1.1.这是我的代码,我已将'c'声明为"final int",但行"c = Integer.parseInt(f.getText());" 我收到错误"无法为最终变量赋值".如果我从声明中删除单词final并将其作为"int c",那么在同一行中我得到一个错误"无法从类中访问局部变量c;需要声明为final".谁能告诉我为什么会这样?
我正在努力获取Firebase存储桶中文件的"长期持久下载链接".我已将此权限更改为
service firebase.storage {
match /b/project-xxx.appspot.com/o {
match /{allPaths=**} {
allow read, write;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的javacode看起来像这样:
private String niceLink (String date){
String link;
// Points to the root reference
StorageReference storageRef = FirebaseStorage.getInstance().getReference();
StorageReference dateRef = storageRef.child("/" + date+ ".csv");
link = dateRef.getDownloadUrl().toString();
return link;
}
Run Code Online (Sandbox Code Playgroud)
当我运行这个时,我得到的uri链接看起来像 com.google.android.gms.tasks.zzh@xxx
问题1.我可以从此获得类似的下载链接:https://firebasestorage.googleapis.com/v0/b/project-xxxx.appspot.com/o/20-5-2016.csv?val = media&token = b5d45a7f-3ab7-4f9b-b661-3a2187adxxxx
当我尝试获取上面的链接时,我在返回之前更改了最后一行,如下所示:
private String niceLink (String date){
String link;
// Points to the root reference
StorageReference storageRef = FirebaseStorage.getInstance().getReference();
StorageReference dateRef = storageRef.child("/" …Run Code Online (Sandbox Code Playgroud) 什么是第二次s.get()返回"ONE"的解释?
String x = "one";
Supplier<String> s = x::toUpperCase;
System.out.println("s.get() = " + s.get());
x = "two";
System.out.println("s.get() = " + s.get());
Run Code Online (Sandbox Code Playgroud)
更新:
比较它:
String x = "one";
Supplier<String> s = () -> x.toUpperCase();
System.out.println("s.get() = " + s.get());
x = "two";
System.out.println("s.get() = " + s.get());
Run Code Online (Sandbox Code Playgroud)
它会抛出一个编译错误.
为了从匿名类访问局部变量,将局部变量声明为final的规则是什么原因?
如果我有一个这样的匿名内部类对象(其中Foo是一个接口):
Foo foo = new Foo(){
@Override
public String hello(Object dummyArg){
return "hello, world.";
}
};
Run Code Online (Sandbox Code Playgroud)
我尝试Foo.hello从这样的jsp 调用:
${foo.hello('blah')}
Run Code Online (Sandbox Code Playgroud)
它抛出:
javax.el.MethodNotFoundException: Unable to find method [hello] with [1] parameters
Run Code Online (Sandbox Code Playgroud)
但如果没有参数:
Bar bar = new bar(){
@Override
public String hello(){
return "hello, world.";
}
};
Run Code Online (Sandbox Code Playgroud)
...
${bar.hello()}
Run Code Online (Sandbox Code Playgroud)
它工作正常.为什么?
这不是7121303的副本.我正在特别询问匿名的内部课程.使用常规类的实例,它可以使用任意数量的参数.
我将一个接口作为匿名实现传递给另一个对象,如下所示:
public interface Interface {
public int convert (int a);
}
public static void main(String[] args) throws IOException, InterruptedException {
final int[] array = {1,6,3,5,7,8,4,0,3};
Interface inter = new Interface() {
public int convert(int a) {
int result = a;
for (int i = 0; i < array.length; i++) {
a=a+array[i];
}
return a;
}
};
SomeObject ty = new SomeObject ();
ty.Test(7, inter);
}
public class SomeObject {
public void Test(int number, Interface inter) {
System.out.println(inter.convert(number));
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:它是如何工作的?如何 …
如果此方法的变量'commonSet'是类级别字段,则以下代码是否会导致相同的问题.如果它是类级别字段,我将不得不在同步块中包装添加设置操作,因为HashSet不是线程安全的.我应该在下面的代码中做同样的事情,因为多个线程正在添加到集合,甚至当前线程可能会继续改变集合.
public void threadCreatorFunction(final String[] args) {
final Set<String> commonSet = new HashSet<String>();
final Runnable runnable = new Runnable() {
@Override
public void run() {
while (true) {
commonSet.add(newValue());
}
}
};
new Thread(runnable, "T_A").start();
new Thread(runnable, "T_B").start();
}
Run Code Online (Sandbox Code Playgroud)
'commonSet'的引用是使用final锁定的.但是在其上运行的多个线程仍然可以破坏集合中的值(它可能包含重复项?).其次,混淆是因为'commonSet'是一个方法级变量 - 它的相同引用将在调用方法的堆栈内存(threadCreatorFunction)和运行方法的堆栈内存 - 这是正确的吗?
有很多与此相关的问题:
但是,我不能看到他们强调线程安全部分这种共享/传递可变性.
我不能在run()方法中使用变量"i".有什么办法吗?
public class Main {
public static void main(String[] args) {
final int M = 100;
final int N = 4;
final int[] array = new int[M];
for(int b = 0; b < M; b++) array[b] = b;
for( int i = 0; i < N; i++) {
new Thread(new Runnable() {
public void run() {
for(int a = i*(M/N);a < (i+1)*(M/N); a++)
System.out.println("Thread "+i+":"+array[a]);
// i -> cannot refer to a non-final variable inside an inner class defined in …Run Code Online (Sandbox Code Playgroud) 我知道方法变量存储在内存的堆栈上但稍有混淆final.我曾浏览像许多环节这无法得到正确的认识?下面是的例子inner class,其中final变量被访问,本地non-final变量不因为它们被存储在stack
class Employee {
public void getAddress(){
final int location = 13;
int notFinalVar = 13;
class Address {
System.out.println (location);
System.out.println (notFinalVar); // compiler error
}
}
Run Code Online (Sandbox Code Playgroud)
更新:刚才开始知道隐藏的字段叫做synthetic field(inner class heap memory area),其中存储了最终变量的副本,所以它最终意味着最终的变量存储在最后Stack memory Area?
为什么java在内部匿名类中引用非final变量是不可能的?简单的答案是"因为它被禁止",但我想知道,为什么他们禁止这种有用的功能?也许Java缺乏某种能力,或者它是以"错误"的方式设计的.我想知道.