How Java handles arguments of a class type.

=====suppose such a method:

 

public static void openFile(String fileName, PrintWriter stream) throws FileNotFoundException

{

   stream = new PrintWriter(fileName);

}

 

 

=====then we want to use it this way:

 

PrintWriter toFile = null;

try

{

   openFile("data.txt", toFile);

}

 

*****After this code is executed, the value of toFile is still null. The file that was opened in the method openFile went away when the method ended. The problem has to do with how Java handles arguments of a class type.

 

"These arguments are passed to the method as memory addresses that cannot be changed. The state of the object at the memory address normally can be changed, but the memory address itself cannot be changed"!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章