-1

when i worked with opencv, i realized something. in some methods in opencv with java API, the method accepts a parameter without returning any values and at running that method, you find the output of the method is presented in the same parameter passed to it. for an example,

Mat mat = new Mat();

while(videoCap.grap()) {

videoCap.retriev(mat);
display(mat);

}

as you see in the code, the method retrieve() returns no values but it somehow process the input parameter and then, that input parameter, after calling retrieve() method, contains different values!

can any one explains how that is possible in java?

rmaik
  • 1,076
  • 3
  • 15
  • 48
  • A very important concept to understand in Java: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – medloh Apr 20 '15 at 22:49

1 Answers1

0

There's nothing impossible about modifying the state of an object that has been passed in as a parameter. Immutable objects can't of course be modified.

It's more common in Java to return a new modified object instead, and this can be a source of bugs if it's not clear that a method is modifying its parameters.

public void modifyMap(Map<String, String> map) {
    map.put("object state", "modified!");
}
Kayaman
  • 72,141
  • 5
  • 83
  • 121