在 Java 中从一种方法调用 String 到另一种方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5131257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call String from one method to another in Java
提问by James
sorry if this is a simple question but I have been trying for quite a while now is it possible to call a string from one method to another...
抱歉,如果这是一个简单的问题,但我已经尝试了很长时间了,现在是否可以将字符串从一种方法调用到另一种方法...
Below I want to call the string fileName
from the method fileName
to fileOutputToFile
. I know I can pass it in but I want to call it from fileOutputToFile
.
下面我想fileName
从方法中调用字符串fileName
到fileOutputToFile
. 我知道我可以传入它,但我想从fileOutputToFile
.
public class outputToFile {
public void fileName(){
String fileName = "Test";
}
public void fileOutputToFile(String hex) throws Exception{
String fileInfo = hex;
try {
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter("myfile.txt", true)));
out.print(fileInfo);
out.print("\n");
out.close();
}catch (IOException e){
}
}
}
回答by coobird
It's not possible to call a local variable which is in another method -- it's an issue of scope.
不可能调用另一个方法中的局部变量——这是一个范围问题。
Therefore, it is not possible to retrieve the fileName
variable from the fileName()
method from the fileOutputToFile
method.
因此,不可能fileName
从fileName()
方法中检索方法中的变量fileOutputToFile
。
One way to "retrieve" the file name would be to return
the file name when the fileName
method is called:
“检索”return
文件名的fileName
一种方法是调用该方法时的文件名:
public String getFileName(){
String fileName = "Test";
return fileName;
}
(Note: I've taken the liberty to rename the method to something that would be closer to the conventions for naming identifiers in Java.)
(注意:我冒昧地将方法重命名为更接近 Java 中命名标识符的约定。)
Then, in the fileOutputToFile
method, the getFileName
method can be called to retrieve the value of the fileName
.
然后,在fileOutputToFile
方法中,getFileName
可以调用该方法来检索 的值fileName
。
It should be noted that in this case, it may actually be better to just use an field (an instance or class variable) rather than calling a separate method to retrieve a file name. Considering the method is just returning a constant String
, a field could hold the value:
应该注意的是,在这种情况下,实际上只使用字段(实例或类变量)而不是调用单独的方法来检索文件名实际上可能更好。考虑到该方法只是返回一个 constant String
,一个字段可以保存该值:
public class OutputToFile {
// Here, we use a class variable.
private static final String FILE_NAME = "Test";
public void fileOutputToFile(String hex) {
// use FILE_NAME field here.
}
}
回答by jbrookover
All variables have scope that is generally limited to the {} enclosure that they are in. So, if you create a variable in one method, it is not accessible in another method unless you pass it in.
所有变量的范围通常仅限于它们所在的 {} 外壳。因此,如果您在一个方法中创建一个变量,则除非您将其传入,否则无法在另一种方法中访问它。
The good news is you can create class level variables that are shared by all methods within a class!
好消息是您可以创建类中所有方法共享的类级变量!
public class OutputToFile {
private String fileName = "Test";
public void fileName() {
System.out.println(fileName);
fileName = "Something Different"
}
public void fileOutputToFile(String hex) {
System.out.println(fileName);
// Do other things with it
}
}
回答by Coleman S
If I understand you correctly, you wish to returna string from fileName().
如果我理解正确,您希望从 fileName()返回一个字符串。
Currently, your implementation of fileName() does nothing. You must add a return statement to return a value: 'return "Test"'
目前,您对 fileName() 的实现什么都不做。您必须添加一个 return 语句来返回一个值:'return "Test"'