Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Java How to Program, 6/e

ISBN:
0-13-148398-6
© 2005
pages: 1576
Buy the Book!
Amazon logo
InformIT logo

The following Java tutorial introduces object serialization--Java's built-in mechanism for manipulating objects as streams of bytes. Object serialization provides the foundation for Java's remote method invocation (RMI) capabilties that enable Java programs that are distributed over a network to invoke each others so-called "remote methods." RMI is used frequently in distributed enterprise applications that are built with Java Enterprise Edition (Java EE). In this tutorial, we demonstrate object serialization by writing entire objects to and reading entire objects from files on disk. This tutorial is intended for students and professionals who are already familiar with Java programming. Download the examples for this tutorial here.

[Notes: This tutorial is an excerpt (Section 14.6) of Chapter 14, Files and Streams, from our best-selling textbook Java How to Program, 6/e. This tutorial may refer to other chapters or sections of the book that are not included here. When you purchase this book you also get free access to the Web-based Java Multimedia Cyber Classroom, 6/e, for six months. The Cyber Classroom includes audio descriptions of the examples in Chapters 1-14, solutions to approximately one-half of the end-of-chapter exercises, interactive true/false self-assessment questions and a searchable Web-based e-book. Permission Information: Deitel, Harvey M. and Paul J., JAVA HOW TO PROGRAM, ©2005, pp. 697-706. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

Introduction to Object Serialization (Continued)

14.6.1 Creating a Sequential-Access File Using Object Serialization

We begin by creating and writing serialized objects to a sequential-access file. In this section, we reuse much of the code from Section 14.5, so we focus only on the new features.

Defining the AccountRecordSerializable Class

Let us begin by modifying our AccountRecord class so that objects of this class can be serialized. Class AccountRecordSerializable (Fig. 14.17) implements interface Serializable (line 7), which allows objects of AccountRecordSerializable to be serialized and deserialized with ObjectOutputStreams and ObjectInputStreams. Interface Serializable is a tagging interface. Such an interface contains no methods. A class that implements Serializable is tagged as being a Serializable object. This is important because an ObjectOutputStream will not output an object unless it is a Serializable object, which is the case for any object of a class that implements Serializable.


Fig. 14.17 AccountRecordSerializable class for serializable objects.
   1  // Fig. 14.17: AccountRecordSerializable.java
2
// A class that represents one record of information.
3
package com.deitel.jhtp6.ch14; // packaged for reuse
4

5
import java.io.Serializable;
6

7
public class AccountRecordSerializable implements Serializable
8
{
9
private int account;
10 private String firstName;
11
private String lastName;
12
private double balance;
13

14
// no-argument constructor calls other constructor with default values
15
public AccountRecordSerializable()
16
{
17
this( 0, "", "", 0.0 );
18
} // end no-argument AccountRecordSerializable constructor
19

20
// four-argument constructor initializes a record
21
public AccountRecordSerializable(
22
int acct, String first, String last, double bal )
23
{
24
setAccount( acct );
25
setFirstName( first );
26
setLastName( last );
27
setBalance( bal );
28
} // end four-argument AccountRecordSerializable constructor
29

30
// set account number
31
public void setAccount( int acct )
32
{
33
account = acct;
34
} // end method setAccount
35

36
// get account number
37
public int getAccount()
38
{
39
return account;
40
} // end method getAccount
41

42
// set first name
43
public void setFirstName( String first )
44
{
45
firstName = first;
46
} // end method setFirstName
47

48
// get first name
49
public String getFirstName()
50
{
51
return firstName;
52
} // end method getFirstName
53

54
// set last name
55
public void setLastName( String last )
56
{
57
lastName = last;
58
} // end method setLastName
59

60
// get last name
61
public String getLastName()
62
{
63
return lastName;
64
} // end method getLastName
65

66
// set balance
67
public void setBalance( double bal )
68
{
69
balance = bal;
70
} // end method setBalance
71

72
// get balance
73
public double getBalance()
74
{
75
return balance;
76
} // end method getBalance
77
} // end class AccountRecordSerializable
Page 1 | 2 | 3 | 4 | 5 | 6 | 7

Tutorial Index