=====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"!
本文探讨了Java中如何处理对象类型的参数。特别强调了当作为内存地址传递时,这些地址指向的对象状态可以改变,但地址本身不可更改的问题。通过具体代码示例展示了方法调用过程中对象引用的变化情况。

被折叠的 条评论
为什么被折叠?



