在 Java 中从一种方法调用 String 到另一种方法

发布于 2024-10-19 13:06:25 字数 751 浏览 1 评论 0原文

抱歉,如果这是一个简单的问题,但我已经尝试了一段时间了,现在是否可以从一个方法调用字符串到另一个方法...

下面我想从该方法调用字符串 fileName fileNamefileOutputToFile。我知道我可以传递它,但我想从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与酒说心事 2024-10-26 13:06:25

不可能调用另一个方法中的局部变量——这是一个范围问题。

因此,无法从 fileOutputToFile 方法的 fileName() 方法检索 fileName 变量。

“检索”文件名的一种方法是在调用 fileName 方法时返回文件名:(

public String getFileName(){
     String fileName = "Test";
     return fileName;
}

注意:我冒昧地重命名了该方法更接近 Java 中命名标识符的约定。)

然后,在 fileOutputToFile 方法中,可以调用 getFileName 方法来检索 代码>文件名。


应该注意的是,在这种情况下,实际上最好只使用字段(实例或类变量),而不是调用单独的方法来检索文件名。考虑到该方法只是返回一个常量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.
    }
}

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.

One way to "retrieve" the file name would be to return the file name when the fileName method is called:

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.)

Then, in the fileOutputToFile method, the getFileName method can be called to retrieve the value of the 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:

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.
    }
}
木格 2024-10-26 13:06:25

所有变量的作用域通常仅限于它们所在的 {} 封装。因此,如果您在一种方法中创建变量,则无法在另一种方法中访问该变量,除非您将其传入。

好消息是您可以创建类级别变量由类中的所有方法共享!

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
    }
}

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
    }
}
一页 2024-10-26 13:06:25

如果我理解正确的话,您希望从 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"'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文