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 / General / April 2007

Tip: Looking for answers? Try searching our database.

Assigning new value to Long instance

Thread view: 
sven - 20 Apr 2007 10:21 GMT
Hi,

in my program I have two objects pointing to a common Long instance. I
want both of them being able to alter the value of the Long instance.
However, a setValue function or something similiar does not seem to be
part of the Long class. Is there any way to change the value of this
long without changing the object reference (rather than wrapping the
Long class and implementing a setter function)?

Any ideas?
Boris Stumm - 20 Apr 2007 10:46 GMT
> Hi,
>
[quoted text clipped - 6 lines]
>
> Any ideas?

Longs are immutable. Maybe try java.util.concurrent.atomic.AtomicLong,
will save you the trouble of having to implement an own wrapper.
sven - 20 Apr 2007 13:32 GMT
> Longs are immutable. Maybe try java.util.concurrent.atomic.AtomicLong,
> will save you the trouble of having to implement an own wrapper.

Thanks, that saves a lot of hassle.
Daniel Pitts - 20 Apr 2007 22:46 GMT
> > Longs are immutable. Maybe try java.util.concurrent.atomic.AtomicLong,
> > will save you the trouble of having to implement an own wrapper.
>
> Thanks, that saves a lot of hassle.

Keep in mind that AtomicLong has extra functionality to deal with
thread-safety.  If you know you don't need thread safety, I would
suggest writing your own wrapper.

More specically, I'm assuming your "long" value actually has a
semantic meaning.  For instance, numberOfSheep.

public class SheepCounter {
  private long numberOfSheep;
  public Long getNumberOfSheep() {
     return numberOfSheep;
  }
  public void count() {
      numberOfSheep++;
  }
  public void setNumberOfSheep(long numberOfSheep) {
    this.numberOfSheep = numberOfSheep;
  }
}

Avoid primative obsession where it makes sense.  You might find that
the class can start taking on more behavior, and you'll end up with
better design.


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.