java一个方法怎样得到另一个方法的变量

这个代码是要点击request按钮后得到rfid扫卡器扫到的id号,扫不同的卡会得到不同的id号,我尝试用static创建一个新的量rfid,然后将id赋值给这个rfid,但我只有在这个方法内用system.out点击request才能显示id,一旦在别的地方用system.out输出,我启动程序还没点request它就输出一个空值,请问要如何才能在另一个方法里得到我点击reques后的id值
第一次编java代码,麻烦说的详细一点。

public static String rfid;
JButton btnRequest = new JButton("Request");
btnRequest.setBounds(153, 252, 89, 27);
btnRequest.addActionListener(new ActionListener()
{

        public void actionPerformed(ActionEvent e)
        {
            int status;
            byte mode = 0x52;
            short[] TagType = new short[1];
            byte bcnt = 0;
            byte snr[] = new byte[16];
            byte len[] = new byte[1];
            byte sak[] = new byte[1];
            
            //Check whether the reader is connected or not
            if(false == ReaderLib.INSTANCE.Sys_IsOpen(g_hDevice[0]))
            {
                lblTips.setText("Please connect the device firstly !");
                return ;        
            }
            
            textUID.setText("");
            //Request card
            status = ReaderLib.INSTANCE.TyA_Request(g_hDevice[0], mode, TagType);//search all card
            if(status != 0) 
            {
                lblTips.setText("TyA_Request failed !");
                return ;
            }
            
            //Anticollision
            status = ReaderLib.INSTANCE.TyA_Anticollision(g_hDevice[0], bcnt, snr, len);//return serial number of card
            if(status != 0 || len[0] != 4) 
            { 
                lblTips.setText("TyA_Anticollision failed !");
                return ;
            }
            String id="";
            for(int i=0; i<4; i++) 
            {
                id = id + String.format("%02X", snr[i]);
            }
            textUID.setText(id);
            rfid=id;
            //Select card
            status = ReaderLib.INSTANCE.TyA_Select(g_hDevice[0], snr, len[0], sak);//lock ISO14443-3 TYPE_A 
            if(status != 0) 
            {
                lblTips.setText("TyA_Select failed !");
                return ;
            }
            
            //Tips
            lblTips.setText("Request card succeed !");
        }
    });

看问题描述,可能是动作异步执行的问题。
也就是你在“别的地方用system.out输出”的时候,截图中“rfid=id”这个赋值操作还没有执行到。
因为异步执行时,它俩不在同一个线程中,即使这两处代码看上去是顺序执行的,但你没法保证先进行“赋值”,后进行“输出”。
可以尝试在截图中赋值的地方,调用“别的地方”的某个方法,也就是回调,来达到你想要的某种效果。

数据获取到之后不能先存库里么,存完再写个方法拿生成的编号去库里查不是更合理么?

你写的就是对的,实在不行可以再参考下这个简单的例子

package com.example.demo003;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Test03 extends JFrame {
    public static String rfid;

    public Test03() throws HeadlessException {
        this.setSize(800, 600);
        this.setLayout(new FlowLayout());
        JButton btnRequest = new JButton("Request");
        btnRequest.setSize(89, 27);
        btnRequest.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 给字段赋予值
                Test03.rfid = "test_input";
            }
        });
        this.add(btnRequest);

        JButton btnRequestOther = new JButton("Print");
        btnRequestOther.setSize(89, 27);
        btnRequestOther.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //再其他任何地方通过Test03.rfid获取值
                System.out.println(Test03.rfid);
            }
        });
        this.add(btnRequestOther);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new Test03();
    }

}

方法一:将变量设置为静态变量 public static,通过类名.变量名访问


```java
class A{
    public static String name = "zhangsan";
}
public class BianLiangDiaoYong {
    public static void main(String[] args) {
        System.out.println(A.name);
    }
}


方法二:如果主函数要调用A的变量,该变量为私有的 private,使用 set() 和 get() 方法,再利用主函数将A实例化,再通过 set() 和 get() 方法调用

```java

class A{
    private int age = 0;
    public int getA() {
        return age;
    }
    public void setA(int age) {
        this.age = age;
    }
}
public class BianLiangDiaoYong {
    public static void main(String[] args) {
        //通过实例化A,再利用实例化对象.get()或者set()获取以及修改值
        A a = new A();
        System.out.println(a.getA());
        a.setA(2);
        System.out.println(a.getA());
    }
}


参考一下呢