如何将局部变量数据存储到同一个class中的全局变量并使用它

How to store the local variables data to global variables in the same class and use it

我面临一个问题,我需要使用同一个 class 中的方法更新字符串数据。我应该创建一个全局变量吗?如果是这样,我如何将更新的字符串数据放置到全局变量中?下面是我的代码,其中添加了注释以说明更多细节。

public class GenerateSummonPDF
{


public void userdata(String p1, String p2, String p3, String p4)
{
    String value1= p1;
    String value2= p2;
    String value3= p3;
    String value4= p4; // all the data here are constantly updating from other class



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

  Document document = new Document();
  try
  {
     PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\Users\User\workspace\enforement system\Summon PDF list\Serial No.pdf"));
     document.open();
     document.add(new Paragraph("")); //i need to print all the data here from the userdata
     document.close();
     writer.close();
  } catch (DocumentException e)
  {
     e.printStackTrace();
  } catch (FileNotFoundException e)
  {
     e.printStackTrace();
  }
 }

有什么解决办法吗?提前感谢您的帮助。

private String value 使用 get/set 方法。 休息你不必改变任何东西。

您可以在这里阅读更多内容:How do getters and setters work?

在这里引用一个答案:

private String myField; //"private" means access to this is restricted

public String getMyField()
{
     //include validation, logic, logging or whatever you like here
    return this.myField;
}
public void setMyField(String value)
{
     //include more logic
     this.myField = value;
}