2

I am passing a int variable called length to JNI in order to modify its value.

But I am not returning the modified value.

My JNIfunction:

int change(int length){
 length =6;
 return something
}

When I use the length value after function call it has the default value itself and not the modified value.

Why am I not getting the modified value?

markusk
  • 6,477
  • 34
  • 39
M.Dev
  • 63
  • 1
  • 6
  • This wouldn't even work in Java only (i.e. if `change(...)` was a Java method). You need to return the changed variable (i.e. `return length;`) – Thomas Sep 17 '18 at 10:00
  • Java is pass-by-value. [read this](https://stackoverflow.com/a/40523/3959856) – Jack Flamp Sep 17 '18 at 10:10
  • 2
    Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Fureeish Sep 17 '18 at 10:19
  • @JackFlamp you could simply flag it as a duplicate if the linked answer clearly answers the question :) – Fureeish Sep 17 '18 at 10:20
  • for arrarys we can getbytearrayelements and setbytearrayregion right?...is there anything like this for this? – M.Dev Sep 17 '18 at 10:24
  • @M.Dev no. Java is *pass by value*. To put it in very simple, beginner-friendly words - each method argument that is a primitive (`int`, `char`, `double`, ...) will be *copied* (*not only primitives, but I consider semantics of copying a reference as beginner non-friendly*) and every change to it won't be visible outside the method. You should either get advantage of the `return` statement, or pass a *reference* to some wrapper, like an `Integer` class – Fureeish Sep 17 '18 at 10:28
  • More to the point, C is pass-by-value, so JNI is too. ("Pass-by-value" is a term of art, so don't try to give it a plain-language or situational interpretation). – Tom Blodget Sep 18 '18 at 16:47
  • @M.Dev If my answer statisfies you can you [mark it](https://stackoverflow.com/help/someone-answers)? thanks in advance! – Simo Sep 24 '18 at 10:18

1 Answers1

2

There are 2 way of doing this:

1) The first one is the simplest possible, just return the length:

int change(int length){
        length = 6;
        return length;
    }

N.B. Java is always pass-by-value!

2) The second one is a bit more tricky:

You have to create a method in order to set and get length value.

Something like this:

public class FooBar { //This is you class with the length var
    private int _length;

    public FooBar(){
        _length = 0;
    }

    //Now we are going to create the SET and GET methods

    public GetLength(){
        return _length;
    }
    public SetLength(int length){
        _length = length;
    }
}

Now that your class looks similar to the above one, we can do this:

int change(int length){
        SetLength(length);
        return something;
    }

If you are calling it from another class you should remember to create the object of the class first as the below example:

FooBar foo1 = new FooBar();

//things and things here

int change(int length){
        foo1.SetLength(length);
        return something;
    }

As you can see, the lenght variable is now private so we need to always use:

  • GetLength in order to retrieve its value.

  • SetLength in order to set a new value.

Every class in java should work like this, just good habits of programming in java!

Simo
  • 955
  • 8
  • 18