gpt4 book ai didi

java - 在多个 JAVA 类之间共享字符串

转载 作者:行者123 更新时间:2023-12-01 18:54:17 26 4
gpt4 key购买 nike

我想在java中的几个类之间共享一个字符串,但是该字符串不是常量,因此通常的public static final方法不起作用。

我的意思是,我更新了字符串的值,这根据使用情况而有所不同。

目前我使用的代码是:

public String NewDestination;

destination = "D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/Uploaded/"; // main location for uploads (CHANGE THIS WHEN USING PREDATOR)
File theFile = new File(destination + "/" + username);
theFile.mkdirs();// will create a sub folder for each user (currently does not work, below hopefully is a solution) (DOES NOW WORK)
System.out.println("Completed Creation of folder");
NewDestination = destination + username + "/";

上面,它是通过将目标+用户名+“/”相加创建的,因此它不能是静态最终的,因为每次登录时都会发生变化,但我仍然需要将值传递给另一个类,我该怎么做?

编辑:

我现在所做的是:

public static String NewDestination; 添加到我的 FileUploadController.java

并在我的 Mybean.java 中添加了 System.out.println(FileUploadController.NewDestination); 并且它仍然打印 null :(

最佳答案

听起来您想要一个 public staticfinal 字段。

但是,这也允许其他类更改它。
最好创建一个 private static 字段和一个 public static getDestination() 方法以允许其他类读取它。

关于java - 在多个 JAVA 类之间共享字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14689721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com