The best one is the one that works ;-)

(The second example won't work because you are calling a method on a null
reference.)

However this example is very different from the one in your first post. It
uses instances of Advertisement instead of String. String are imutable. The
question here (provided you take off the line that that set the reference to
null in the while loop) is : can you safely reuse the Advertisement
instance. From you code, we can guess that you can't. In short, you can
reuse it when you have finished using it. Here, you are storing the instance
in a vector. I guess you want to access the objects in that vector later.
So, what you will get is a vector full of references to your unique
instance. The state of this unique instance will correspond to the values
set in the last iteration of the loop.

I guess that the differrences between objects and references is not fully
clear to you.

The line :

        Advertisement advertisement = null;

creates a reference of type Advertisement named advertisement. A reference
is a specific type of "handle" because it allow us to handle the object.

= null says that this reference doesnt point to any object. (This is only
needed for non member references).

The line :

        advertisement = new Advertisement();

does two things :

        1) it creates an instance of Advertiement.
        2) it make the advertisement reference to point to this new object.

Clearly, the reference is not the object.

Various methods are then called on the reference. This means that this
methods may affect the fields of the object corresponding to the reference.
(Here, the affected fields are probably some ID and text field).

When you affect your object to a vector, you are just creating a new kind of
handle. It is as if you could name your object using to different phrases :

        1) "The object pointed by the advertisement reference".

        2) "The last object in the advVector vector".

In Java, this translate to

        1) advertisement

        2) (Advertisment)(advVector.get(last))

where last is the index of the last element.

If you then call methods on the advertisement reference like :

        advertisement.setAdvertisementID (id);
        advertisement.setAdvertisementText(rs.getString(AD_TEXT));

it is exactly the same as if you called the methods on the vector elements
like :

        ((Advertisement)advVector.get(last)).setAdvertisementID(id);

((Advertisement)advVector.get(last)).setAdvertisementText(rs.getString(AD_TE
XT))

In fact, you are modifying the fields of all elements in the vector because
all these handles corresponds to the same object.

Here is an example to demonstrate this :

import java.util.*;

public class TestHandles {

    public static void main(String[] args) {
        Advertisement advertisement = new Advertisement();
        Vector advVector = new Vector();
        int id = 0;
        while (id++ < 3) {
            advertisement.setAdvertisementID(id);
            advertisement.setAdvertisementText("This is text of ad no " +
id);
            advVector.add(advertisement);
        }

        for (int i = 0; i < 3; i++) {
            System.out.print("Element no: ");
            System.out.print(i);
            System.out.print("\tElement ID: ");

System.out.print(((Advertisement)advVector.get(i)).getAdvertisementID());
            System.out.print("\tElement text: ");

System.out.print(((Advertisement)advVector.get(i)).getAdvertisementText());
            System.out.println();        }
     }
}

class Advertisement {
    private int id;
    private String text;

    public void setAdvertisementID(int id) {
        this.id = id;
    }

    public void setAdvertisementText(String text) {
        this.text = text;
    }

    public int getAdvertisementID() {
        return this.id;
    }

    public String getAdvertisementText() {
        return this.text;
    }
}

This example will display :

Element no: 0   Element ID: 3   Element text: This is text of ad no 3
Element no: 1   Element ID: 3   Element text: This is text of ad no 3
Element no: 2   Element ID: 3   Element text: This is text of ad no 3


BTW, there is no need to call your method setAdvertisementID and
setAdvertisementText.

advertisement.setAdvertisementID (id);

is redundant. the following is as clear and much shorter :

advertisement.setID (id);

Pierre-Yves


-----Message d'origine-----
De : A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[EMAIL PROTECTED]]De la part de
Sushil Singh
Envoy� : jeudi 14 juin 2001 00:39
� : [EMAIL PROTECTED]
Objet : Re: Basic Object Reference Question


Hi All:

Thanks for all your comments.

Another better example is:
First Senario
=========
     Advertisement advertisement = null;
     Vector advVector = new Vector();
     while (rs.next())
     {
          advertisement = new Advertisement();
          advertisement.setAdvertisementID (id);
          advertisement.setAdvertisementText(rs.getString(AD_TEXT));
          advVector.add(advertisement);
      }
Secound Senario
=========
     Advertisement advertisement = new Advertisement();
     Vector advVector = new Vector();
     while (rs.next())
     {
          advertisement = null;
          advertisement.setAdvertisementID (id);
          advertisement.setAdvertisementText(rs.getString(AD_TEXT));
          advVector.add(advertisement);
      }

Which one is better?  In first case, I am creating instance of
Advertistemnet
object in a loop, but in secound example its outside the loop.

Awaiting feedback.

Thanks

Sushil


Jason Kilgrow wrote:

> It depends on whether or not you are planning to use catID for something
else.
> If your concern is that a new string object will be made in the while
loop,
> don't worry about it. That's going to happen either way. I think the issue
is
> going to come down to reusing catID. If you aren't going to reuse it (you
> aren't in your examples), then I say go with the first snippet. Because
whether
> you are going to make a new string object either way.
>
> --- Sushil Singh <[EMAIL PROTECTED]> wrote:
> > Hi,
> >
> > I am having a basic object reference question. Consider the following
> > code snippet:
> >       while (rootRs.next())
> >       {
> >
> > categories.addElement(getCategory(rootRs.getString("cat_id")));
> >       }
> >
> > Another code snippet:
> >      String catID = "";
> >       while (rootRs.next())
> >       {
> >            catID = rootRs.getString("cat_id");
> >            categories.addElement(getCategory(catID));
> >       }
> >
> > Now my question is which one is better?  When we say
> >
> > categories.addElement(getCategory(rootRs.getString("cat_id")));
> >
> > then we are getting a reference to the resultset string object or its
> > new object. If its a new String object, then the while loop will be
> > creating n no of objects throughout the loop!!!
>
> __________________________________________________
> Do You Yahoo!?
> Get personalized email addresses from Yahoo! Mail - only $35
> a year!  http://personal.mail.yahoo.com/
>
>
___________________________________________________________________________
> 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

___________________________________________________________________________
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

Reply via email to