Possible Duplicate:
Is Java pass by reference?
when I used some java class like (Integer, File, Boolean)
if I pass the instance as an argument to a function and try to change its value
after I use this value outside function, the value remains unchanged.
for example:
private void run(){
File tmpFile;
setFile(tmpFile);
System.out.println(tmpFile.getAbsolutePath()); //error tmpFile is null
}
private void setFile(File xFile){
xFile = jFileChooser.getSelectedFile(); // this returned object file
}
The short answer is that Java uses call-by-value, not call-by-reference.
In your
setFilemethod, your assignment toxFileonly changes the local variable. It does not change the variabletmpFilein therun()method.You should write the code so that
setFilereturns a value; e.g.(Note: I changed the method name, because a method called
setXXXthat doesn’t actually set anything is gratuitously misleading.)