Hi,
as indicated before, the confusion stems from not knowing what Java passes
by reference, and what it passes by value. For your example, a reference to
a PrintWriter is passed, which is then used. So your code works as
expected.
But there seems to be also confusion on what happens when you change the
reference itself in a method.
Everything that inherits from Object is passed by reference, this includes
String, PrintWriters, anything else despite the basic simple types (all
types not starting with a capital letter), i.e. int, float, double, byte
etc. Note, that Integer, Float, Double are objects and are passed by
reference.
an example:
void callingMethod() {
....
SomeObject myObject=new SomeObject(); //could be any subclass of
object, but method must take the appropriate argument
myMethod(myObject);
...
}
void myMethod(SomeObject myObject) {
myObject.changeState();
}
So, any change that changeState does to myObject are visible in the
callingMethod. This includes changes you might do by directly accessing the
members of myObject, i.e. instead of myObject.changeState(), you can also
do myObject.publicValue=newValue. All these changes are visible back in
callingMethod.
On the contrary, if you pass a simple type, these changes are not visible,
i.e.
double a=3.0;
myMethod(a);
System.out.println(a);
public void myMethod(double b) {
b=2.0;
}
your output will still be 3.0. If you pass an object as in the first
example, you don't really pass the object, but pass a reference to the
object. Thus, inside myMethod you're still refering to the same object.
Instead, if you pass a simple type like in the last example, you really
don't pass a, but 3.0 (pass by value). Inside myMethod another variable b
is initialized with the value from the calling method, i.e. with 3 but then
it is set to 2. But once you're outside the method again, b is not
available anymore.
Note, that if you change the reference itself, the situation is much the
same as with pass-by value, i.e.
public void myMethod(MyObject myObject) {
myObject=new MyObject();
}
will create the same problem, i.e. the newly created object is not returned
back to the calling method, and is not available anymore once myMethod has
exited. Thus, such code does not make any sense.
If you want to do things like that, there are two ways:
1)
public MyObject myMethod(MyObject myObject) {
... //here, you could do something with myObject, if you don't, you
don't need the argument in this method.
return new MyObject();
}
2)
public void myMethod(ModifiableObject modObject) {
modObject.value=new MyObject();
}
where ModifiableObject is defined by
public class ModifiableObject {
public Object value;
public ModifiableObject(Object newValue) {value=newValue;}
}
and you call this method via myMethod(new ModifiableObject(myObject))
Regards
HEIKO
[EMAIL PROTECTED]
Balogh Andras <[EMAIL PROTECTED]> on 13-08-99 09:23:08
Please respond to "A mailing list for discussion about Sun Microsystem's
Java Servlet API Technology."
<[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]
cc: (bcc: Heiko Grussbach/NMG/CRP-HT)
Subject: Re: will this code work?
Hi all,
I am really confused now about this messages. I think somebody (not me)
should clarify this problem .
I think Chris is right.
Here is a servlet example:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ProbaServlet extends HttpServlet
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
PrintWriter toClient=null;
try{
res.setContentType("text/html");
toClient=res.getWriter();
printHtml(toClient);
} catch(Exception e) { toClient.println("Error:"+e);}
toClient.close();
}
private void printHtml(PrintWriter client){
client.println("<html>");
client.println("<head></head>");
client.println("<body bgcolor=\"#FFFFFF\">");
client.println("How is that this is works");
client.println("</body>");
client.println("</html>");
}
}//end servlet
Now if the PrintWriter object toClient is not the same as the
PrintWriter object client than it means that everything written in
the client object will NOT go to browser back.
But it DOES.
So what about somebody Finalizing this problem?
----- Original Message -----
From: Chris Pratt <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, August 13, 1999 9:46 AM
Subject: Re: will this code work?
> Wrong, Wrong, Wrong. Your example is correct, but that's because you're
> changing the value of the reference, not the value of the String
(remember
> String's are immutable). Java uses Pass by Reference for Objects and
Pass
> by Value for intrinsic types (int, float, char, etc). This has been a
major
> source of confusion with the Java detractors saying that the entire
contents
> of each Object must be copied to the stack since Java is "pointerless".
If
> you don't believe me, try this program. I bet you get "i = 10"
> (*Chris*)
>
> class Obj {
> int i;
>
> public Obj (int i) {
> this.i = i;
> }
>
> public int add (int j) {
> i += j;
> return i;
> }
>
> public String toString () {
> return String.valueOf(i);
> }
>
> }
>
> public class test {
>
> public static void process_me(Obj o) {
> o.add(5);
> }
>
> public static void main(String[] args) {
> Obj obj = new Obj(5);
> obj.add(5);
> System.out.println("obj = " + obj);
> }
>
> }
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html