— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet404/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   400   401   402   403   404   405   406   407   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

import java.io.*;
public class Pond implements Serializable {
private Duck duck = new Duck();
public static void main (String[] args) {
Pond myPond = new Pond();
try {
FileOutputStream fs = new FileOutputStream(“Pond.ser”);
ObjectOutputStream os = new ObjectOutputStream(fs);

os.writeObject(myPond);
os.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
Class Pond has one instance 
variable, a Duck.
When you serialize myPond (a Pond 
object), its Duck instance variable 
automatically gets serialized.
public class Duck {
// duck code here
}
Yikes!! Duck is not serializable! 
It doesn’t implement Serializable, 
so when you try to serialize a 
Pond object, it fails because the 
Pond’s Duck instance variable 
can’t be saved. 
Pond objects can be serialized.
File Edit Window Help Regret
% java Pond 
java.io.NotS
erializableE
xception: Du
ck

at Pond.main
(Pond.java:1
3)
When you try to run the main in class Pond:


serialization 
and
file I/O
you are here
4
439
Mark an instance variable as transient 
if it can’t (or shouldn’t) be saved.
If you want an instance variable to be skipped by the 
serialization process, mark the variable with the 
transient 
keyword.
import java.net.*;
class Chat implements Serializable {
transient String currentID;

String userName;
// more code
}
transient says, “don’t 
save this variable during 
serialization, just skip it.” 
userName variable 
will be saved as part 
of the object’s state 
during serialization.
It’s hopeless, 
then? I’m completely 
screwed if the idiot who 
wrote the class for my instance 
variable forgot to make it 
Serializable? 
If you have an instance variable that can’t be saved because 
it isn’t serializable, you can mark that variable with the 
transient keyword and the serialization process will skip right 
over it. 
So why would a variable not be serializable? It could be 
that the class designer simply forgot to make the class 
implement Serializable. Or it might be because the object 
relies on runtime-specific information that simply can’t be 
saved. Although most things in the Java class libraries are 
serializable, you can’t save things like network connections, 
threads, or file objects. They’re all dependent on (and 
specific to) a particular runtime ‘experience’. In other words, 
they’re instantiated in a way that’s unique to a particular run 
of your program, on a particular platform, in a particular 
JVM. Once the program shuts down, there’s no way to bring 
those things back to life in any meaningful way; they have to 
be created from scratch each time.



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   400   401   402   403   404   405   406   407   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish