Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > unnamed object creation

Reply
Thread Tools

unnamed object creation

 
 
Mattia Belletti
Guest
Posts: n/a
 
      10-20-2003
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: http://www.velocityreviews.com/forums/(E-Mail Removed)
* IRC: BluShine - site(s): http://cs.unibo.it/~mbellett
* Linux registered user 299762 @ Linux registered machine 213003
*/

 
Reply With Quote
 
 
 
 
Jason Us
Guest
Posts: n/a
 
      10-20-2003
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[i], 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
 
Reply With Quote
 
 
 
 
Dobromir Gaydarov
Guest
Posts: n/a
 
      10-21-2003

"Jason Us" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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[i], 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


 
Reply With Quote
 
Paul Lutus
Guest
Posts: n/a
 
      10-21-2003
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[i], 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

 
Reply With Quote
 
Jason Us
Guest
Posts: n/a
 
      10-22-2003
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[i], 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 <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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[i], 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to (batch) set EXIF date taken and IPTC creation date and creation time for photos with filenames YYMMDDHHMMSS#.jpg? guercheLE@gmail.com Digital Photography 1 10-04-2005 07:15 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Unnamed compound object as "buffer" arguments Eric Laberge C Programming 7 09-09-2005 01:31 PM
Can't shutdown without Ending unnamed process Bryan McCaul Computer Support 3 09-20-2004 10:08 PM
Unnamed "Formal Parameter" Mysooru C++ 3 07-24-2003 06:11 AM



Advertisments