You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by neal <ne...@yahoo.com> on 2002/09/16 08:48:08 UTC

OT - Java variables - a copy or a reference?

Does anyone know exactly what Java is doing when one creates a new variable
by referring to an existing variable? Does it simply create a reference to
the original object, or does it completely copy the object referred to? And,
is the answer the same when dealing with Application-scope variables?

I have created a hash on init of my application and placed transformer
objects in it for all my XSLT files thereby caching those my XsLT.  This is
then placed into the application object:

	ServletContext application = getServletContext();
		application.setAttribute(...);

Now, in order to refer to these items, in subsequent servlets I must refer
to the application object again.  If I do this on each page:

	ServletContext application = getServletContext();
	Hashtable myHash = application.getAttribute("myHash");


Am I simply creating a refernce to that object, or creating a temporary
copy?  Will this create a local copy or is it still a reference to the
Application Singeton instance?

Thanks.
Neal


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: OT - Java variables - a copy or a reference?

Posted by neal <ne...@yahoo.com>.
Cool.  Thanks for the explanation!  :)

Neal


-----Original Message-----
From: Willis Boyce [mailto:wboyce@panix.com]
Sent: Monday, September 16, 2002 12:00 AM
To: Tomcat Users List
Cc: wboyce@panix.com
Subject: Re: OT - Java variables - a copy or a reference?


Neal,

Java creates references.  In other words this code:

Object a = new Object();
Object b = a;

creates one Object instance and two references that point to it.  That's
why it's sometimes misleading to "name" an object by its reference.  You
could say that the Object instance in this example is called "a," but it
might be equally valid to call it "b" depending on the circumstances.

This code is functionally identical:

Object a = new Object();
myServletContext.setAttribute("a",a);
Object b = myServletContext.getAttribute("a");

If you really want to make a *copy* then you should make the class
implement Cloneable and create a public clone method that invokes
super.clone.  Here is my standard Cloneable implementation:

class MyCloneableClass implements Cloneable
{
    public Object clone()
    {
        try
        {
            return super.clone();
        }
        catch (CloneNotSupportedException x)
        {
            // super.clone throws this if this class does not implement
            // Cloneable, but obviously this one does, so we can consume
            // it here to avoid putting it in the throws clause of this
            // method.
        }
    }
}

Avoid the tempation to implement clone yourself by doing new
MyCloneableClass and copying each field individually.  That will make it
difficult for others to inherit from your class, as its clone method only
creates instances of MyCloneableClass and not instances of
MyCloneableSubclass.  The clone method of Object (invoked via super.clone)
does the right thing for subclasses.

W


On Sun, 15 Sep 2002, neal wrote:

> Does anyone know exactly what Java is doing when one creates a new
variable
> by referring to an existing variable? Does it simply create a reference to
> the original object, or does it completely copy the object referred to?
And,
> is the answer the same when dealing with Application-scope variables?


--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: OT - Java variables - a copy or a reference?

Posted by Willis Boyce <wb...@panix.com>.
Neal,

Java creates references.  In other words this code:

Object a = new Object();
Object b = a;

creates one Object instance and two references that point to it.  That's
why it's sometimes misleading to "name" an object by its reference.  You
could say that the Object instance in this example is called "a," but it
might be equally valid to call it "b" depending on the circumstances.

This code is functionally identical:

Object a = new Object();
myServletContext.setAttribute("a",a);
Object b = myServletContext.getAttribute("a");

If you really want to make a *copy* then you should make the class
implement Cloneable and create a public clone method that invokes
super.clone.  Here is my standard Cloneable implementation:

class MyCloneableClass implements Cloneable
{
    public Object clone()
    {
        try
        {
            return super.clone();
        }
        catch (CloneNotSupportedException x)
        {
            // super.clone throws this if this class does not implement
            // Cloneable, but obviously this one does, so we can consume
            // it here to avoid putting it in the throws clause of this
            // method.
        }
    }
}

Avoid the tempation to implement clone yourself by doing new
MyCloneableClass and copying each field individually.  That will make it
difficult for others to inherit from your class, as its clone method only
creates instances of MyCloneableClass and not instances of
MyCloneableSubclass.  The clone method of Object (invoked via super.clone)
does the right thing for subclasses.

W


On Sun, 15 Sep 2002, neal wrote:

> Does anyone know exactly what Java is doing when one creates a new variable
> by referring to an existing variable? Does it simply create a reference to
> the original object, or does it completely copy the object referred to? And,
> is the answer the same when dealing with Application-scope variables?


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>