简体   繁体   English

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

[英]How to make the value of a variable inside a Java Private method available/visible to another java file?

I have a Private method in a java file within which I have a variable called maxRate. 我在java文件中有一个Private方法,其中有一个名为maxRate的变量。 I need the value of maxRate in another java file to compare it with another variable minRate. 我需要在另一个java文件中使用maxRate的值来将它与另一个变量minRate进行比较。

How do I make the maxRate variable visible to another java file? 如何使maxRate变量对另一个java文件可见?

My code is as follows A.java (in a diff package) 我的代码如下A.java(在diff包中)

public class A{

  private Integer maxRate = null;

  private modelAndView dConfig(int a,string b,string c, string d, string e){
    Map<String, PropConf> map = getConf(b,c,d,e);
    PropConf propConf = map.get(getKey(a));
    Integer maxRate = propConf.getMaxRate();
  }
}

Problem: I need the value of maxRate in B.java which resides in a different package. 问题:我需要在B.java中存在maxRate的值,它位于不同的包中。

Progress: 进展:

As of now as per suggestions, I declared maxRate as private Integer maxRate on the top within the public class A{ and have also declared a getter method in A.java as follows, 截至目前,根据建议,我将maxRate声明为公共类A {中的顶部的私有Integer maxRate,并且还在A.java中声明了一个getter方法,如下所示,

public Integer getMaxRate(){
return this.maxRate
}

then I tried calling getMaxRate from within B.java as follows, B.java 然后我尝试从B.java中调用getMaxRate,如下所示,B.java

public ModelAndView save() {

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

The f value was printed as null. f值打印为null。

You can't get to the internals of the method. 你无法进​​入方法的内部。 Either make it a class level field with an associated getter, or return it from your method. 使其成为具有关联getter的类级别字段,或者从方法返回它。

You need to define the variable outside of your private function and define it as public. 您需要在私有函数之外定义变量并将其定义为public。 You can also define it outside of your private function (keeping it private) and then add a public getter and/or setter. 您还可以在私有函数之外定义它(保持私有),然后添加公共getter和/或setter。 Since the more obvious public getting/setter methods, and public class variable options are covered in other answers, here is much less common and more convoluted way to extracting a private field from a class. 由于更明显的公共获取/设置器方法以及公共类变量选项在其他答案中都有所涉及,因此从类中提取私有字段的方式不那么常见且更复杂。 First the variable must be declared outside of the private function fields inside functions aren't even accessible from reflection (maybe with a project that reads Java byte-code): 首先,变量必须在私有函数字段之外声明,函数内部甚至无法从反射中访问(可能使用读取Java字节代码的项目):

Field f = obj.getClass().getDeclaredField("maxRate");
f.setAccessible(true);
Integer secretInt = (Integer) f.get(obj);

You can't access a method variable from another method except you are calling the method2 inside dConfig and passing the variable directly to method2. 除了在dConfig中调用method2并将变量直接传递给method2之外,您无法从另一个方法访问方法变量。

private modelAndView dConfig(){

    method2(maxRate);

}

A possible solution to your problem could be the following... 您的问题的可能解决方案可能是以下......

You can make the variable maxRate a private class variable: 您可以将变量maxRate设为私有类变量:

private Integer maxRate = 9;

the add a public getter method: 添加一个公共getter方法:

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

Edit for updates: 编辑更新:

Your code should look like this: 您的代码应如下所示:

public class A {

  private Integer maxRate;

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

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

As i said in my comment, seems you are missing the initialize part of your variable. 正如我在评论中所说,似乎你错过了变量的初始化部分。

You really need to revisit Object Oriented programming concepts, from the grounds up. 真的需要从头开始重新审视面向对象的编程概念。 I seriously suspect, that your problem is deeper than a variable visibility issue, and is rather related to an improper class structure... 我严重怀疑,你的问题比可变的可见性问题更深,而且与不正确的类结构有关......

To directly deal with this scenario, without going deeper, this approach can work: 要直接处理这种情况,不要更深入,这种方法可以工作:

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
}

EDIT 编辑

I see you have trouble using this construct. 我发现你在使用这个结构时遇到了麻烦。 Please heed my advice, and read what OOP is all about. 请听取我的意见,并阅读OOP的全部内容。 You are trying to run withoout not even being capable of sitting up! 你试图在没有能力坐起来的情况下跑步!

From a function in class B you need to do this: 从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); 
}

As you didn't disclose your full code, I can only guess what you try to achieve... 由于您没有透露您的完整代码,我只能猜测您尝试实现的目标......

Also some advices, as you seem to need guidance: 还有一些建议,因为你似乎需要指导:

You can create a getter method to get the value of that variable but you you need it to declare as instance variable. 您可以创建一个getter方法来获取该变量的值,但是您需要将其声明为实例变量。

public class A {
    private Integer maxRate;

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

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

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

相关问题 如何在另一个方法中的java中返回该方法的值 - How to return the value of the method in java inside another method 如何在 Java 中的另一个 class 中通过另一种方法给出变量值 - How to give a variable value through another method in another class in Java 如何在 Java 模块中创建模块私有方法 - How to make module private method in Java modules 如何在Java中创建对同一包中的其他类不可见的(私有)类? - How to make a (private) class in java that is not visible to other classes in the same package? java中如何改变私有变量的值 - How to change the value of private variable in java 如何使用 java 中的另一个 class 更改 class 中的变量值? - How to change the value of variable inside a class using another class in java? 如何替换Java变量中可用的“ \\ t”值 - how to substitute the “\t” value available in a variable in java 在Java中的另一个方法中检索局部变量值 - Retrieve local variable value in another method in java 从 SpringBoot Java 中的其他私有方法中的私有方法中的变量获取信息 - get information from a variable inside a private method from other private method in SpringBoot Java java - 如何在一个类中的方法内进行循环以调用另一个类中的另一种方法n Java? - How do I make a loop inside a method in one class to call on another method in another class n Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM