• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Storing Data in an object

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I store user input information within an object and have it retained for later use? I need to present a screen and accept contact information Name, Address, Phone number etc. I do not want to store it in a text file. There can be multi contact records/object. When the application ends I want to take the information entered and store it for later display and or update.
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i don't know if i got you right:

if you need to store the information as long as the application is running, simply store it as a class member and read it when you need it. just make sure that your object doesn't get garbage collected... ;-)

if you need your information to be persistent over the scope of your application, you'll need to write it somewhere to. serialization/deserialization would be the easieast way in my eyes.

hope it helps,

jan.
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ronald Lansing:
Can I store user input information within an object and have it retained for later use? I need to present a screen and accept contact information Name, Address, Phone number etc.



Probably the best idea in a client application (i.e. not a distributed application running from a J2EE server) would be to use an object to store your details. For a server-based application, you would need to consider some form of database persistent storage as well.

You might consider a class called something like Person, which stores all the required data for an individual (their name etc...)

You would probably then want to maintain a pool (collection) of those objects, enabling you to store, retrieve and delete them as required. One way would be to maintain a singleton List of the objects - for example, you might have a static member of a PersonStorage class which is a List containing all Person instances currently in the application. This won't be the best way to access them. Instead, if you have one field which can be used to identify that Person uniquely, you could use it like a primary key in a database. Instead of implementing the pool as a List, implement it as a Map, using the primary key as the key and the Person as the value. That way, you can quickly and easily retrieve a single Person object by their primary key without having to shuffle through an entire List.


When the application ends I want to take the information entered and store it for later display and or update.



You obviously need to code the application to recognise a shut-down condition (for example, the main GUI window being closed, or an "exit" command received from the console), then all you need to do is copy the PersonStorage Map (or List) into file storage. You can either use Java's serialization mechanism (make sure the Person class implements Serializable for this to work), or use some other persistent storage device like XML or even a custom plain-text file. My personal preference is XML; I've never liked Java Serialization that much because, although it's quick and easy to use, changes (code updates/modifications) to the class file can cause the de-serialization process to fail. I like XML because it is completely decoupled from the Java platform, doesn't rely on the version of the class (permitting multiple updates to occur seamlessly), and XML allows for portability between applications written in different languages.

Good luck!
 
Don't touch me. And dont' touch this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic