1

I'm experiencing a weird behaviour using a java method inside an EJB class.

I have a couple of Integer, declared as follows:

Integer decimalDigit = null;
Integer decimalExponent = null;

I pass them to the following method, along with other parameters.

public void GetPrecision(Currency cur, String nodeCode, Integer decimalDigit, Integer decimalExponent) {

    decimalDigit = new Integer(cur.getDecimalDigit());
    decimalExponent = new Integer(cur.getDecimalExponent());

    if (!CommonHelper.isNullOrEmptyOrBlank(nodeCode)) {

        Node tempNode = nodeListProvider.getNodeList().get(nodeCode);

        if (tempNode != null && tempNode.getDecimalDigit() != null) {
            decimalDigit = (int) tempNode.getDecimalDigit();
            decimalExponent = 0;
        }
    }
}

The 2 objects are correctly istantiated inside the method using the new operator and they stay like that until the end of the call but, as soon as i get out, the 2 variables are again null.

I cannot explain this behaviour, any hint?

Thanks in advance

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
rekotc
  • 595
  • 1
  • 10
  • 21
  • 1
    Java is pass by value. Changes made to the actual parameter passed in are not saved when the method exits. – David Choweller Feb 07 '17 at 15:03
  • 1
    Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Deltharis Feb 07 '17 at 15:35

3 Answers3

1

Arguments are passed by value but the method receives a copy of the references, not directly the references of your Integer.
So any assignation of the parameters inside the method will not change the value referenced by the references you have passed.

Your method should rather return a structure instance (array or a custom class) that contains the two Integer.

Besides a method named GetPrecision() is expected to return something.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • mmm, i don't exactly understand the difference between passing the 2 Integer and passing a custom class that contains the 2 Integer. Aren't both of them objects? – rekotc Feb 07 '17 at 15:12
  • No. I said your method should return something : either an array `Integer[]` that contains new values of decimalDigit and decimalExponent or a custom class that contains two Integer fields. – davidxxx Feb 07 '17 at 15:15
  • Ok, but i tried to create a foo class containing the 2 Integers, i pass this foo object as an argument, and it works correctly, the 2 Integer values are still not null once the method is over. So i don't understand the difference between a custom object passed as an argument and the Integer object i used in my first example. – rekotc Feb 07 '17 at 15:19
  • 1
    The difference is that the reference to the custom object you passed in was not changed either. What was changed was the integers inside the object that the reference referred to. – David Choweller Feb 07 '17 at 15:27
  • 1
    So you cannot change references, but you can change the objects that the references refer to. – David Choweller Feb 07 '17 at 15:27
  • 2
    David Choweller is right. But the idea I proposed is simply returning the custom object in the method so that you can retrieve it after the invocation in the method. `Precision precision = getPrecision(cur, nodeCode, decimalDigit, decimalExponent);`. Change from `void` to `Precision` and return a Precision in the method. – davidxxx Feb 07 '17 at 15:33
  • yes, i think i'll go for your solution davidxxx, thanks! :) – rekotc Feb 07 '17 at 15:35
0

if cur.getDecimalDigit() and cur.getDecimalExponent() both yielded null,decimalDigit and decimalExponent will still come out null upon any subsequent value set.so check the cur.getDecimalDigit() and cur.getDecimalExponent() values.

Rbk
  • 72
  • 1
  • 11
0

You're referring to the local variables the scope of which ends with the method with these lines:

decimalDigit = new Integer(cur.getDecimalDigit());
decimalExponent = new Integer(cur.getDecimalExponent());

because you have declared them as your formal parameters.

Rewrite the method as such to solve the issue:

public void GetPrecision(Currency cur, String nodeCode, Integer a, Integer b) {

//method body   

}

It's bad practice to use identifiers like a and b but you get the problem.

SirVirgin
  • 153
  • 1
  • 3
  • 10