在 Java 中从一种方法调用 String 到另一种方法
抱歉,如果这是一个简单的问题,但我已经尝试了一段时间了,现在是否可以从一个方法调用字符串到另一个方法...
下面我想从该方法调用字符串 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){
}
}
}
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
.
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){
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不可能调用另一个方法中的局部变量——这是一个范围问题。
因此,无法从
fileOutputToFile
方法的fileName()
方法检索fileName
变量。“检索”文件名的一种方法是在调用
fileName
方法时返回
文件名:(注意:我冒昧地重命名了该方法更接近 Java 中命名标识符的约定。)
然后,在
fileOutputToFile
方法中,可以调用getFileName
方法来检索代码>文件名。
应该注意的是,在这种情况下,实际上最好只使用字段(实例或类变量),而不是调用单独的方法来检索文件名。考虑到该方法只是返回一个常量
String
,字段可以保存该值: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 thefileName()
method from thefileOutputToFile
method.One way to "retrieve" the file name would be to
return
the file name when thefileName
method is called:(Note: I've taken the liberty to rename the method to something that would be closer to the conventions for naming identifiers in Java.)
Then, in the
fileOutputToFile
method, thegetFileName
method can be called to retrieve the value of thefileName
.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:所有变量的作用域通常仅限于它们所在的 {} 封装。因此,如果您在一种方法中创建变量,则无法在另一种方法中访问该变量,除非您将其传入。
好消息是您可以创建类级别变量由类中的所有方法共享!
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!
如果我理解正确的话,您希望从 fileName() 返回一个字符串。
目前,您的 fileName() 实现不执行任何操作。您必须添加返回语句以返回值:'return“Test”'
If I understand you correctly, you wish to return a string from fileName().
Currently, your implementation of fileName() does nothing. You must add a return statement to return a value: 'return "Test"'