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.
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.
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.
Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / AmazonAmazon UK )