java - 如何访问 if block 内声明的变量?

标签 java variables

我有这个代码:

public void onPlayerInteract(PlayerInteractEvent event) {
    final Action action = event.getAction();
    if (action == Action.LEFT_CLICK_BLOCK){
        Location l1 = event.getClickedBlock().getLocation();
    } else if (action == Action.RIGHT_CLICK_BLOCK) {
        Location l2 = event.getClickedBlock().getLocation();
    }

    Thread t = new Thread() {
        @Override
        public void run() {
            while(true) {
                try {
                    Thread.sleep(1000*60*60);
                    //Insert code here
                } catch (InterruptedException ie) {
                }
            }
        }
    };
    t.start();

如何从//在此处插入代码访问l1

最佳答案

只需在 if block 之外声明 l1,否则它是该 block 的本地变量。

关于java - 如何访问 if block 内声明的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10761928/

相关文章:

java - 安卓系统错误: Cannot find symbol method

Java:Collection-如何创建多列

c - 返回指向局部结构的指针

java - ToString 和输出问题

variables - 在 Squeak 中创建新的伪变量

c - C中的变量定义是什么意思

java.io.FileNotFoundException 打开失败 : EEXIST (File exists) Android 11

java - Android 的英语到印地语音译

Java插件扩展点

bash - 如何访问参数中变量的内容?