unnamed object creation

Discussion in 'Java' started by Mattia Belletti, Oct 20, 2003.

  1. Jason Us wrote:
    > I assume this creates an array of Phenomenon objects. But if their is
    > no name given to the array - how is it accessed? What is is scope? I
    > thought that each of these objects would only have scope within the
    > loop block.
    >
    > Jason


    You're right; the only way I can think this code is useful, is if the
    Phenomenon constructor "register" itself (through "this" pointer) to
    some other class. Even if it seems a bit weird to me... some pattern I miss?

    --
    /**
    * Mattia Belletti - Undergraduate student @ cs.unibo.it
    * ICQ: 33292311 - email:
    * IRC: BluShine - site(s): http://cs.unibo.it/~mbellett
    * Linux registered user 299762 @ Linux registered machine 213003
    */
    Mattia Belletti, Oct 20, 2003
    #1
    1. Advertising

  2. Mattia Belletti

    Jason Us Guest

    Thanks in advance all

    I have question about the following piece of code from UML Distilled:

    public class PhenomenonType
    {
    public void setPhenomenon(String[] names)
    {
    for (int i =0; i < names.length; i++)
    new Phenomenon( names, this );
    }

    }

    I assume this creates an array of Phenomenon objects. But if their is
    no name given to the array - how is it accessed? What is is scope? I
    thought that each of these objects would only have scope within the
    loop block.

    Jason
    Jason Us, Oct 21, 2003
    #2
    1. Advertising

  3. "Jason Us" <> wrote in message
    news:...
    > Thanks in advance all
    >
    > I have question about the following piece of code from UML Distilled:
    >
    > public class PhenomenonType
    > {
    > public void setPhenomenon(String[] names)
    > {
    > for (int i =0; i < names.length; i++)
    > new Phenomenon( names, this );
    > }
    >
    > }
    >
    > I assume this creates an array of Phenomenon objects. But if their is
    > no name given to the array - how is it accessed? What is is scope? I
    > thought that each of these objects would only have scope within the
    > loop block.


    No, it does not create an array, since there is no array declaration.
    It creates a number of Phenomenon object and does not care to store
    references to them.

    One possible use of something like that is if the Phenomenon is an active
    object that will notify us (using the 'this' parameter we sent him) about
    something.

    If Phenomenon is passive it can still register with us, but this will be bad
    design - you do not phone somebody to ask him about his phone number, do
    you?

    Regards,
    Dobromir
    Dobromir Gaydarov, Oct 21, 2003
    #3
  4. Mattia Belletti

    Paul Lutus Guest

    Jason Us wrote:

    > Thanks in advance all
    >
    > I have question about the following piece of code from UML Distilled:
    >
    > public class PhenomenonType
    > {
    > public void setPhenomenon(String[] names)
    > {
    > for (int i =0; i < names.length; i++)
    > new Phenomenon( names, this );
    > }
    >
    > }
    >
    > I assume this creates an array of Phenomenon objects.


    No, on the strength of the posted code only, any created objects are not
    stored anywhere, and the passed String array references can't be changed,
    so the loop doesn't acutually do anything to the source array. But since
    you haven't posted the Phenomenon class, or what may remain of the present
    class, we cannot tell what does happen.

    > But if their is
    > no name given to the array - how is it accessed? What is is scope? I
    > thought that each of these objects would only have scope within the
    > loop block.


    We cannot tell unless you provide the Phenomenon class and a complete
    listing.

    --
    Paul Lutus
    http://www.arachnoid.com
    Paul Lutus, Oct 21, 2003
    #4
  5. Mattia Belletti

    Jason Us Guest

    Sorry guys. Here is the rest of the code.

    public class PhenomenonType extends DomainObject {
    private vector _phenomena = new Vector();

    public PhenomenonType (String name) {
    super(name);
    }

    void friendPhenomenonAdd(Phenomenon newPhenomenon) {
    //RESTRICTED: only used by Phenomenon
    _phenomena.addElement(newPhenomenon);
    }

    public void setPhenomenon(String[] names)
    {
    for (int i =0; i < names.length; i++)
    new Phenomenon( names, this );
    }

    public Enumeration phenomena() {
    return _phenomena.elements();
    }
    }


    public class Phenomenon extends DomainObject {
    private PhenomenonType _type;
    public Phenomenon (String name, PhenomenonType type) {
    super(name);
    _type = type;
    _type.friendPhenomenonAdd(this);
    }

    public PhenomenonType phenomenonType() {
    return _type;
    }
    }

    Paul Lutus <> wrote in message news:<>...
    > Jason Us wrote:
    >
    > > Thanks in advance all
    > >
    > > I have question about the following piece of code from UML Distilled:
    > >
    > > public class PhenomenonType
    > > {
    > > public void setPhenomenon(String[] names)
    > > {
    > > for (int i =0; i < names.length; i++)
    > > new Phenomenon( names, this );
    > > }

    >
    > > }
    > >
    > > I assume this creates an array of Phenomenon objects.

    >
    > No, on the strength of the posted code only, any created objects are not
    > stored anywhere, and the passed String array references can't be changed,
    > so the loop doesn't acutually do anything to the source array. But since
    > you haven't posted the Phenomenon class, or what may remain of the present
    > class, we cannot tell what does happen.
    >
    > > But if their is
    > > no name given to the array - how is it accessed? What is is scope? I
    > > thought that each of these objects would only have scope within the
    > > loop block.

    >
    > We cannot tell unless you provide the Phenomenon class and a complete
    > listing.
    Jason Us, Oct 22, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mysooru

    Unnamed "Formal Parameter"

    Mysooru, Jul 24, 2003, in forum: C++
    Replies:
    3
    Views:
    449
    Mike Wahler
    Jul 24, 2003
  2. Razmig K
    Replies:
    3
    Views:
    678
    John L Fjellstad
    Sep 5, 2003
  3. Jordi Vilar
    Replies:
    5
    Views:
    2,278
    Jonathan Turkanis
    Feb 18, 2004
  4. Eric Laberge

    Unnamed compound object as "buffer" arguments

    Eric Laberge, Sep 4, 2005, in forum: C Programming
    Replies:
    7
    Views:
    323
    Michael Wojcik
    Sep 9, 2005
  5. jon wayne
    Replies:
    9
    Views:
    704
    Jim Langston
    Sep 22, 2005
Loading...

Share This Page