Usage of transient keywords

Source: Internet
Author: User

Author: scruffybear

Release time: 1/11/2006

Company: watchdata

If any reprint is available, please indicate the source and maintain the integrity of the Article. Thank you!

Java serialization provides a great mechanism for storing the object State. To put it bluntly, serialization stores the object state on the hard disk, you can read and use it as needed. However, when storing the object status, we sometimes need to not store specific object data during serialization. At this time, the transient keyword will be used. You can use the transient keyword to define a specific data domain of the class. For the underlying Java virtual machine, the transient type variable is not a permanent state of the class.

The following is the experiment code for the transient Keyword:

Import java. util .*;
Import java. Io .*;

Public class logginginfo implements java. Io. serializable
{
Private date loggingdate = new date ();
Private string uid;
Private transient string PWD;

Logginginfo (string user, string password)
{
Uid = user;
Pwd = password;
}
Public String tostring ()
{
String Password = NULL;
If (Pwd = NULL)
{
Password = "not set ";
}
Else
{
Password = PWD;
}
Return "Logon info:/N" + "User:" + uid +
"/N logging Date:" + loggingdate. tostring () +
"/N password:" + password;
}

Public static void main (string ARGs []) {
Logginginfo loginfo = new logginginfo ("Mike", "mechanic ");
System. Out. println (loginfo. tostring ());
Try
{
Objectoutputstream o = new objectoutputstream (
New fileoutputstream ("loginfo. Out "));
O. writeobject (loginfo );
O. Close ();
}
Catch (exception e) {// deal with exception}
System. Out. println ("Hello world! ");
}

Try
{
Objectinputstream in = new objectinputstream (
New fileinputstream ("loginfo. Out "));
Logginginfo loginfo1 = (logginginfo) in. readobject ();
System. Out. println (loginfo1.tostring ());
}
Catch (exception e ){
// Deal with exception
}

}
}

The loginfo. Out file is generated for serialization to store Class Object Data. After being stored, the file is read. The read content is as follows.
The output result is as follows:
Logon info:
User: Mike
Logging Date: Mon Oct 30 21:39:58 CST 2006
Password: Mechanics
Logon info:
User: Mike
Logging Date: Mon Oct 30 21:39:58 CST 2006
Password: Not Set

We can see that the Read Password is mechanic, but the Read Password is not set, because it is not stored on the hard disk during serialization, because PWD is defined as transient.

The transient keyword also produces side effects. See the following code:

Public class guestlogginginfo implements java. Io. serializable
{
Private date loggingdate = new date ();
Private string uid;
Private transient string PWD;

Guestlogginginfo ()
{
Uid = "guest ";
Pwd = "guest ";
}
Public String tostring ()
{
// Same as abve
}
}

The read PWD or not set, that is, the default initialization does not work.

In addition, record the Writing Method of Java code throwing exceptions.
For example, I want to throw a null pointer exception:
If (comparebuffer = NULL ){
Nullpointerexception E = new nullpointerexception ("The comparebuffer is null! ");
Throw E;
}

In fact, nullpointerexception is inherited from runtimeexception, while runtimeexception is inherited from exception.

Over!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.