Stalwart
Stalwart

Reputation: 59

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. I need the value of maxRate in another java file to compare it with another variable minRate.

How do I make the maxRate variable visible to another java file?

My code is as follows A.java (in a diff package)

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.

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,

public Integer getMaxRate(){
return this.maxRate
}

then I tried calling getMaxRate from within B.java as follows, 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.

Upvotes: 0

Views: 3182

Answers (5)

ppeterka
ppeterka

Reputation: 20736

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. You are trying to run withoout not even being capable of sitting up!

From a function in class B you need to do this:

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:

Upvotes: 0

araknoid
araknoid

Reputation: 3125

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.

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:

private Integer maxRate = 9;

the add a public getter method:

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.

Upvotes: 0

Jason Sperske
Jason Sperske

Reputation: 30446

You need to define the variable outside of your private function and define it as public. You can also define it outside of your private function (keeping it private) and then add a public getter and/or 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):

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

Upvotes: 0

codenut
codenut

Reputation: 221

You can create a getter method to get the value of that variable but you you need it to declare as instance variable.

public class A {
    private Integer maxRate;

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

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

Upvotes: 0

Hiery Nomus
Hiery Nomus

Reputation: 17779

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.

Upvotes: 2

Related Questions