如何使Java私有方法中的变量值对另一个Java文件可用/可见?

如何使Java私有方法中的变量值对另一个Java文件可用/可见?,java,Java,我在java文件中有一个私有方法,其中有一个名为maxRate的变量。我需要另一个java文件中的maxRate值,以将其与另一个变量minRate进行比较 如何使maxRate变量对另一个java文件可见 我的代码如下 java(在一个diff包中) 然后我尝试从B.java中调用getMaxRate,如下所示, B.java public ModelAndView save() { A a = new A(); Integer f = a.getMaxRate(); logger.debu

我在java文件中有一个私有方法,其中有一个名为maxRate的变量。我需要另一个java文件中的maxRate值,以将其与另一个变量minRate进行比较

如何使maxRate变量对另一个java文件可见

我的代码如下 java(在一个diff包中)

然后我尝试从B.java中调用getMaxRate,如下所示, B.java

public ModelAndView save() {

A a = new A();
Integer f = a.getMaxRate();
logger.debug("F is" +f); }

f值被打印为null。

您需要在私有函数之外定义变量,并将其定义为public。您还可以在私有函数之外定义它(保持私有),然后添加公共getter和/或setter。由于其他答案中涵盖了更明显的公共获取/设置方法和公共类变量选项,因此这里是从类中提取私有字段的一种不太常见且更复杂的方法。首先,变量必须在私有函数字段之外声明,函数内部的字段甚至不能从反射中访问(可能使用读取Java字节码的项目):


您无法进入该方法的内部。将其设置为具有关联getter的类级字段,或从方法返回。

除非在dConfig中调用method2并将变量直接传递给method2,否则无法从其他方法访问方法变量

private modelAndView dConfig(){

    method2(maxRate);

}
您的问题的可能解决方案如下所示

您可以将变量maxRate设置为私有类变量:

private Integer maxRate = 9;
添加公共getter方法:

public Integer getMaxRate() {
    return this.maxRate;
}
编辑以获取更新:

您的代码应该如下所示:

public class A {

  private Integer maxRate;

  public A() {
    this.maxRate = 9; //Initialize of the variable
  }

  public Integer getMaxRate() {
    return this.maxRate;
  }
}
正如我在评论中所说,您似乎缺少了变量的初始化部分。

您确实需要从头开始重新审视概念。我严重怀疑,您的问题不仅仅是一个可变可见性问题,而是与不正确的类结构有关

public class A {
    private Integer maxRate;

    public Integer getMaxRate() {
        return this.maxRate;
    }

    private modelAndView dConfig(){
        this.maxRate = 9;
    } 
}
为了直接处理这种情况,无需深入,这种方法可以奏效:

public class MyClass {
    //instance variable: this can be different in 
    //each instances of this class. Now we initialize it to null
    private Integer maxRate=null; 

    private modelAndView dConfig(){

        //IMPORTANT!!!!!! Don't **declare** a new variable maxRate!
        // Your code Integer maxRate = 9; **declares a new one**
        // that would hide your class variable with a enw variable, 
        // whose scope is the method only

        maxRate = 9; //this sets the instance variable
        //this only happens when running the method
        //... I assume there are quirte some code here

    }

    /**
     * This function is available for each MyClass instance, and retrieves the
     * coresponding maxRate value.
     */
    public Integer getMaxRate() { 
         return maxRate;
    }

    //... other code
}
编辑

我发现您在使用此结构时遇到了问题。请听从我的建议,阅读OOP的全部内容。你试着不坐起来就跑

从类B中的函数中,您需要执行以下操作:

public ModelAndView save() {

    A a = new A(); //created class A -- initialized maxRate to null!!
    //now need to run the method that sets the instance variable:
    a.dConfig(); // maxRate set to 9, or whatever.

    //don't use single char variable names! also, f is for float, if something!
    Integer maxRate = a.getMaxRate();

    //surprise. 
    logger.debug("F is" +f); 
}
由于你没有透露你的全部代码,我只能猜测你试图实现什么

还有一些建议,因为您似乎需要指导:

  • 阅读Java中的变量作用域
  • 阅读OOP
  • 坚持
  • 了解干净的代码

您可以创建一个getter方法来获取该变量的值,但需要将其声明为实例变量

public class A {
    private Integer maxRate;

    public Integer getMaxRate() {
        return this.maxRate;
    }

    private modelAndView dConfig(){
        this.maxRate = 9;
    } 
}

您不能访问在类之外的私有类中初始化的变量,您需要将其初始化为全局变量。除了公共getter和setter以及公共字段之外,唯一的其他方法是使用reflection。我可以问您更熟悉哪种语言?如果我理解了您期望Java具有的语言功能,它可能会帮助我回答您的问题。但如果是教学,为什么不好好教呢?考虑到所需的知识存在于教师身上,做对与做错所需的努力量完全相同!我以前对公共领域也有同样的感觉,我想JavaScript已经宠坏了我:PAlso,当问题性质完全不同时,请不要向新手程序员建议反思。OP显然没有意识到这将给他带来的力量,而且可能会造成比他和他未来的同事更多的麻烦。这个问题不是关于如何在不能更改的类中获得私有变量的值。您的方法是最后的手段,如果有机会,在任何情况下都不应使用!您是否希望使用代码,这样做?如果使字段类级别不可行,那么返回值是一个很好的选择。在Java中没有全局变量之类的东西。您所指的是一个实例变量!当我试图将maxRate声明为private时,我得到一个错误,声明“允许非法修改参数maxRate only final”。@stallwart aranoid意味着以private开头的行将插入类的方法之外。请看我的答案,这已经很详细了。谢谢阿拉诺德/佩特卡。实际上我尝试了上述方法,但是当我试图通过logger语句从b.java打印maxRate值时,我得到的只是null,而不是我在a.java中检索的确切maxRate值。a.java中我的私有方法和maxRate变量的定义如下:私有模型和视图dConfig(){maxRate=pConfig.getMaxRate();}从注释中读取问题并不是那么容易。你能用你的进步、你所做的改变和你所遇到的问题来编辑你的问题吗?嗨,阿拉诺德,我已经编辑了我的问题,并根据你的建议提到了进展。
public ModelAndView save() {

    A a = new A(); //created class A -- initialized maxRate to null!!
    //now need to run the method that sets the instance variable:
    a.dConfig(); // maxRate set to 9, or whatever.

    //don't use single char variable names! also, f is for float, if something!
    Integer maxRate = a.getMaxRate();

    //surprise. 
    logger.debug("F is" +f); 
}
public class A {
    private Integer maxRate;

    public Integer getMaxRate() {
        return this.maxRate;
    }

    private modelAndView dConfig(){
        this.maxRate = 9;
    } 
}