简体   繁体   English

如何访问在另一个Java类的方法中声明的变量?

[英]How to access a variable which is declared in a method belonging to another Java class?

I have recently started trying my hands at Java and I am stuck at this problem. 我最近开始尝试使用Java,但是我一直陷入这个问题。 I have two Java files called Main_file.java and Helper.java. 我有两个Java文件,分别称为Main_file.java和Helper.java。 The Helper.java file contains a String variable called the name, which I wish to access form my Mainfile.java and assign to a string variable x. Helper.java文件包含一个名为name的字符串变量,我希望从Mainfile.java中访问该变量并将其分配给字符串变量x。 The files look soemthing like this. 文件看起来像这样。

Main.java Main.java

public class Mainfile{

    Helper myhelper =new MyHelper();

    public void create_func(){
        String x =  /* assign the value name from the helper file */;
    }

Helper.java Helper.java

public class Helper{

    public void add_name() {
        String name = "New_name";
    }
}

But this does not seem to work. 但这似乎不起作用。 I am not really sure if the method I am trying is right or wrong. 我不确定我尝试的方法是对还是错。 Could somebody please help me? 有人可以帮我吗? Thank you in advance. 先感谢您。

You can not directly access a local variable of a method of another class. 您不能直接访问另一个类的方法的局部变量。 You can do it by making the method returning the object and access it by calling the method by an object of the class. 您可以通过使方法返回对象并通过类的对象调用该方法来进行访问来实现。 This is how you can: 这样可以:

public class Mainfile{
Helper myhelper =new Helper();

public void create_func(){

    String x =  myhelper.add_name();
} 
}

public class Helper{

  public String add_name(){
      String name = "New_name";
      return name;
}
}

The variable name you create in your Helper class is not class-member but only a member which exists in the method add_name() 您在Helper类中创建的变量name不是class-member,而是仅存在于方法add_name()

If you want a class member you'll have to create it like this: 如果您想要一个班级成员,则必须像这样创建它:

public class Helper{
    String name = "New_name";
}

then you can access it like this: 那么您可以像这样访问它:

public class MainFile{

    Helper myHelper = new Helper();

    public void create_func(){
        String x =  myHelper.name;
    }
}

Many people will say that class-members "have" to be private, so it might be nicer to create getters and setters for the class member: 许多人会说,班级成员“必须”是私有的,因此为班级成员创建吸气剂和塞特气可能会更好:

public class Helper{
    private String name = "New_name";
    public String getName() {
        return name;
    }
    public void setName(String newName) {
        name = newName;
    }
}

public class MainFile{

    Helper myHelper = new Helper();

    public void create_func(){
        String x =  myHelper.getName();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我已将其声明为Java中类的成员变量,是否可以访问另一个方法中一个方法的变量值? - Can i access the value of a variable of one method in another method if i have declared it as a member variable of the class in java? 访问在属于同一类 java 的方法中的构造函数中初始化/声明的变量 - Accessing a variable that is initialized/declared in the constructor within a method belonging to the same class java Java反射通过另一个类中的接口声明的对象的访问方法 - Access method of a object declared through interface in another class by java reflection 如何在同一 class 中的 java 中访问存储在另一种方法中的变量 - How to access a variable stored in another method in java in the same class Java:如何访问在switch语句中声明的变量 - Java: how to access a variable which was declared inside a switch statement 访问Java私有类内部声明的变量 - Access the variable declared inside the private class in java 如何访问另一个 class 的方法变量? - How to access a variable of method of an another class? java - 如何访问另一个文件中另一个类的方法,该文件在java中的同一个包中? - How to access a method of another class in another file which is in same package in java? 如何访问另一个方法中定义的变量(在Java中)? - How to access a variable defined in another method (In java)? 如何在for循环中更改属于某个类的变量? (JAVA) - How do I alter a variable belonging to a class in a for loop? (Java)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM