Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2004

Tip: Looking for answers? Try searching our database.

immutable v. mutable objects in composition

Thread view: 
javac - 29 Apr 2004 18:37 GMT
object "LifeForm" has an immutable object "Location"

I want "change" the immutable Location object by generating a new
Location, but it ain't working the way i wants it to.  how do i get
this to work, pls?  it creates a new object, but never discards the
original, as i want it to.

also, what's the best forum for a question such as this?

i've adapted examples from:
http://java.sun.com/docs/books/performance/  
http://java.sun.com/docs/books/effective/

my code:
http://www.geocities.com/cjavacjava/src/

javac@mail.com
Christophe Vanfleteren - 29 Apr 2004 18:45 GMT
> object "LifeForm" has an immutable object "Location"
>
> I want "change" the immutable Location object by generating a new
> Location, but it ain't working the way i wants it to.  how do i get
> this to work, pls?  it creates a new object, but never discards the
> original, as i want it to.

You would give LifeForm a setLocation method.

something like lifeform.setLocation(new Location("North Pole"));

> also, what's the best forum for a question such as this?

This newsgroup is just fine for this question, but you need to work on your
posting skills. Like what directory should we be supposed to look in?

> i've adapted examples from:
> http://java.sun.com/docs/books/performance/
> http://java.sun.com/docs/books/effective/
>
> my code:
> http://www.geocities.com/cjavacjava/src/

Just try to post a small inline workable example.

Look at http://www.physci.org/codes/sscce.jsp for tips on how to ask better
questions, so we can help you more easily.

Signature

Kind regards,
Christophe Vanfleteren

javac - 01 May 2004 20:24 GMT
I see what you mean.  presently VectorOfLifeforms does that with:

   private static void moveLife(LifeForm life) {
       System.out.println("..moveLife..");
       Location location = life.getLocation();
       location.valueOf(1, 1, 1);
   }//moveLife

and presumably this is the problem.  this method _seems_ to create a
new Location object.  why doesn't it supersede the old Location?  if
it doesn't, how do I replace the old Location with the new Location,
using this technique?  if it can be done, should it?

thanks,

javac@mail.com

[..]
> You would give LifeForm a setLocation method.
>
> something like lifeform.setLocation(new Location("North Pole"));
[..]
Christophe Vanfleteren - 01 May 2004 20:34 GMT
> I see what you mean.  presently VectorOfLifeforms does that with:
>
[quoted text clipped - 6 lines]
> and presumably this is the problem.  this method _seems_ to create a
> new Location object.

Why do you think a new Location is created? All is see is you getting a
reference to an existing Location object. Then you use that Location and
change some internal value.
Unless a new Location is created in the life.getLocation() method, which
would be very form.

Btw, that life.valueOf() method doesn't seem to do what its name implies to
me. Does it return anything? If it doesn't, consider giving it another
name).

> why doesn't it supersede the old Location?  if
> it doesn't, how do I replace the old Location with the new Location,
> using this technique?  if it can be done, should it?

public class LifeForm {

private Location location;

public void setLocation(Location location){
       this.location = location;
}

public Location getLocation() {
       return this.location;
}
}


//inside some other method:
Location newLocation = new Location();
lifeform.setLocation(newLocation);
//lifeform now has a new Location.

Signature

Kind regards,
Christophe Vanfleteren

javac - 03 May 2004 23:11 GMT
i've made cosmetic changes, but still have the same difficulty:

[..]
> Why do you think a new Location is created? All is see is you getting a
> reference to an existing Location object. Then you use that Location and
> change some internal value.
> Unless a new Location is created in the life.getLocation() method, which
> would be very form.
  ^^^^^^^^^^^^^^^^^^^
I don't understand what "very form" means...?

   public Location getLocation() {
       System.out.println("..getLocation..");
       return location;
   }//getLocation
   
   public void setLocation(int x, int y, int z) {
       System.out.println("..setLocation..");
       location.valueOf(x, y, z);
   }//setLocation

both methods, in fact, return a new Location object.  when I run (?)
the program I see output indicating there exists Locations with
(x,y,z) values of (1,1,1).  unfortunately, I can't figure out where
these objects are, because a later test of the LifeForm objects shows
only the original position.

Location _should_ be immutable, for what it's worth.

> Btw, that life.valueOf() method doesn't seem to do what its name implies to
> me. Does it return anything? If it doesn't, consider giving it another
> name).
>  
[..]
   public static LifeForm valueOf() {
       System.out.println("..valueOf..");
       return new LifeForm();
   }//valueOf
   
it returns a new LifeForm, as per page 9 of
http://java.sun.com/docs/books/effective/ to quote: "valueOf--returns
an instance that has, loosely speaking, the same value as its
parameters.  Static factory methods with this name are effectively
type-conversion operators."  uhhh, not sure how to compare that to
getInstance.  could I have some guidance with regard to the name here,
pls?

thank you very much,

javac@mail.com
http://www.geocities.com/cjavacjava/
now with real links! and new java!
Christophe Vanfleteren - 04 May 2004 00:13 GMT
> i've made cosmetic changes, but still have the same difficulty:
>
[quoted text clipped - 6 lines]
>    ^^^^^^^^^^^^^^^^^^^
> I don't understand what "very form" means...?

Typo, that should have read "very bad form".

>     public Location getLocation() {
>         System.out.println("..getLocation..");
[quoted text clipped - 5 lines]
>         location.valueOf(x, y, z);
>     }//setLocation

You say location.valueOf() creates a new Location, but you don't assign the
returned object to anything. And setLocation definitely does not return a
new Location object, since you declare it to return void.

> both methods, in fact, return a new Location object.  when I run (?)
> the program I see output indicating there exists Locations with
[quoted text clipped - 13 lines]
>         return new LifeForm();
>     }//valueOf

Now it makes more sense, valueOf is a static method. To avoid creating the
kind of confusion I had, it is much better to only call static methods on
the class itself, and not on an instance.

If you had typed Lifeform form = Lifeform.valueOf(1,1,1); it would have been
clear what valueOf did (if you follow the naming conventions and start
classnames with a capital and instances with a lower case anyway).
 
> it returns a new LifeForm, as per page 9 of
> http://java.sun.com/docs/books/effective/ to quote: "valueOf--returns
[quoted text clipped - 3 lines]
> getInstance.  could I have some guidance with regard to the name here,
> pls?


Signature

Kind regards,
Christophe Vanfleteren

javac - 04 May 2004 07:33 GMT
[..]

> You say location.valueOf() creates a new Location, but you don't assign the
> returned object to anything. And setLocation definitely does not return a
> new Location object, since you declare it to return void.
[..]
> Now it makes more sense, valueOf is a static method. To avoid creating the
> kind of confusion I had, it is much better to only call static methods on
[quoted text clipped - 3 lines]
> clear what valueOf did (if you follow the naming conventions and start
> classnames with a capital and instances with a lower case anyway).

my mistake.  "return new LifeForm();" should read "return new
lifeForm;"  thanks for spotting the error.

if i understood java, it'd be so much easier to ask questions ;)

i'll change valueOf() from static to member methods for both classes
and fix the typo.  i certainly need to rethink how setLocation() and
valueOf() interact.  i don't understand how to make location.valueOf()
assign the returned object to the _correct_ lifeForm object.

with some thought, i might get this running tonight :)

thanks for all the help,

javac@mail.com
http://www.geocities.com/cjavacjava/
Chris Smith - 04 May 2004 21:16 GMT
> my mistake.  "return new LifeForm();" should read "return new
> lifeForm;"  thanks for spotting the error.

The latter statement is a syntax error.  Are you sure that's what it
"should" be?

> i'll change valueOf() from static to member methods for both classes
> and fix the typo.

I think you're going the wrong way.  It would be terribly non-standard
and confusing to provide a non-static method called "valueOf".  Perhaps
you just need to call it as a static method, and use it as a result.  
Such as:

   location = Location.valueOf(...);

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

javac - 03 May 2004 23:20 GMT
[..]
> public class LifeForm {
>
[quoted text clipped - 14 lines]
> lifeform.setLocation(newLocation);
> //lifeform now has a new Location.

eureka!  i think i understand now.  i need the "this.location =
location" to simultaneously link the new location to the LifeForm and
un-link the old location :)  yes?

   public Location getLocation() {
       System.out.println("..getLocation..");
       return location;
   }//getLocation

do i need to change "return location" to "return this.location"?  I'm
presuming I need to make _both_ changes, not simply one.  conceptually
a bit difficult for me, as to why i'd need to "return this.location".
the first suggestion makes more sense to me.

thanks,

javac@mail.com
http://www.geocities.com/cjavacjava/
Christophe Vanfleteren - 04 May 2004 00:17 GMT
> [..]
>> public class LifeForm {
[quoted text clipped - 19 lines]
> location" to simultaneously link the new location to the LifeForm and
> un-link the old location :)  yes?

Yes, but is is not called "linking". You just say that you assign a new
Location to the location variable.

>     public Location getLocation() {
>         System.out.println("..getLocation..");
[quoted text clipped - 5 lines]
> a bit difficult for me, as to why i'd need to "return this.location".
> the first suggestion makes more sense to me.

No, "return location" and "return this.location" means the same thing.
I only use "this.location = location" in the setLocation method because the
parameter is also called location. Some people would consider this bad
form, and use something like this instead:

public void setLocation(Location newLocation){
 location = newLocation;
}

Signature

Kind regards,
Christophe Vanfleteren

Jeff Schwab - 29 Apr 2004 18:53 GMT
> object "LifeForm" has an immutable object "Location"
>
> I want "change" the immutable Location object by generating a new
> Location, but it ain't working the way i wants it to.  how do i get
> this to work, pls?  it creates a new object, but never discards the
> original, as i want it to.

Here's how English grammar works:  A "pronoun" is a word used to refer
to a previously used noun.  The original noun is called the pronoun's
"antecedent."  Your first use of the pronoun "it" seems to refer to the
noun "Location," such that I take

  it ain't working the way i wants it to

to mean

  The new Location isn't working the way I want.

OK, you have conveyed the existence of a problem, although the problem
has not been defined.  Your next use of "it" has no clear antecedent:

  it creates a new object, but never discards the
  original, as i want it to.

What creates a new object?  What do you mean by "discard?"  Do you mean
that a garbage object is not being collected?

I tried to follow the link you claimed led to your source code:

  my code:
  http://www.geocities.com/cjavacjava/src/

However, all I saw was a list of directories:

  Name                    Last modified       Size  Description

  Parent Directory        19-Apr-2004 17:48      -
  ecosystem3/             07-Apr-2004 22:23      -
  ecosystem4/             09-Apr-2004 21:59      -
  ecosystemOne/           04-Apr-2004 23:00      -
  ecosystemTwo/           04-Apr-2004 23:09      -

Please post a small program that shows the problem you have encountered.
 Explain what behavior is not matching your expectations, and what
thoughts you have as to possible causes of the situation.
VisionSet - 29 Apr 2004 22:52 GMT
>    it ain't working the way i wants it to
>
> to mean
>
>    The new Location isn't working the way I want.

to mean

The new 'Location' is not working the way I would like.

--
Mike W
javac - 01 May 2004 19:47 GMT
it's not working the way I want in that, while the new Location object
is certainly created, it seems to have no connection to the LifeForm
object.  if there is a connection, I don't know how to find out if
there is a connection.

that is, what happened to the "old" Location object?  is the new
Location object just floating around?  there should be a one-to-one
relation between Location objects and LifeForm objects.  "old"
Location objects should be set (somehow) for garbage collection,
either implicitly or explicitly (doesn't matter at this point).

I hope my question's clearer.  I didn't post any code because I wasn't
sure which class (or combination of classes) is the source of the
problem, never mind which method.  there are only three classes,
though.

http://www.geocities.com/cjavacjava/src/ecosystem4/  is the most
recent location for my source code.  as I make changes, it'd be
../ecosystem5/ etc.  sorry, i thought that'd be self explanatory.

anyhow, now that I've hopefully clarified the question, can anyone
help me with it?  FWIW if VectorOfLifeForms.java's comments aren't
explanatory, please let me know.

thanks,

javac@mail.com
javac - 04 May 2004 23:53 GMT
finally got it to work, thanks to everyone who responded :)

javac@mail.com
http://www.geocities.com/cjavacjava/

or

http://www.geocities.com/cjavacjava/src/ecosystem7/
for the most current version


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.