What is a transient variable

A transient variable is a variable that may not be serialized.

  

Editorial / Best Answer

sivagopal  

  • Member Since Jun-2009 | Jun 8th, 2009


A variable that won't be allowed for object serialisation. so that the state of the value will always be defaulted after the deserialisation.
For example a variable x's value is set to 9, it's default value is '0' say, when the object has been serialized having x's value 9, after deserialisation will be defaulted to '0'

Use: In Java, serialization of an object allowed only when all the underlying objects of the object that is currently under serialization contains has a relationship, will not be allowed, some times the developer has no choice of implementing serializable marker interface on some classes as the classes might have been arrived from a third party and the developer has no control over them, but the developer needs to serialize the object's state, then the developer has the choice of marking the objects that not serializable as transient.

Showing Answers 1 - 56 of 56 Answers

Roby

  • May 23rd, 2005
 

Transient instance fields are neither saved nor restored by the standard serialisation mechanism. You have to handle restoring them yourself.

  Was this answer useful?  Yes

Mark

  • Jun 11th, 2005
 

Declaring a Transient Variable  
 
You use the transient keyword to indicate to the Java virtual machine that the indicated variable is not part of the persistent state of the object. Variables that are part of the persistent state of an object must be saved when the object is archived.  
 
At this time, the transient marker is ignored by the Java runtime system. Future releases of the Java system will use the transient marker to implement various object archiving functions.  
 
Like other variable modifiers in the Java system, you use transient in a class or instance variable declaration like this:  
 
This statement declares an integer variable named hobo that is not part of the persistent state of the TransientExample class.

  Was this answer useful?  Yes

Roko

  • Jun 11th, 2005
 

Transient variable can't be serialize. For example if a variable is declared as transient in a Serializable class and the class is written to an ObjectStream, the value of the variable can't be written to the stream instead when the class is retrieved from the ObjectStream the value of the variable becomes null

  Was this answer useful?  Yes

vijay

  • Jun 27th, 2005
 

1 what is a transient variable  
A transient variable is a variable that may not be serialized.  
 
2 which containers use a border Layout as their default layout  
The window, Frame and Dialog classes use a border layout as their default layout.  
 
3 Why do threads block on I/O  
Threads block on i/o (that is enters the waiting state) so that other threads may execute while the i/o Operation is performed.  
 
4 How are Observer and Observable used  
Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that i...  
 
5 What is synchronization and why is it important  
With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a ...  
 
6 Can a lock be acquired on a class  
Yes, a lock can be acquired on a class. This lock is acquired on the class's Class object..  
 
7 What's new with the stop(), suspend() and resume() methods in JDK 1.2  
The stop(), suspend() and resume() methods have been deprecated in JDK 1.2.  
 
8 Is null a keyword  
The null value is not a keyword.  
 
9 What is the preferred size of a component  
The preferred size of a component is the minimum component size that will allow the component to display normally.  
 
10 What method is used to specify a container's layout  
The setLayout() method is used to specify a container's layout.  

  Was this answer useful?  Yes

preeti

  • Jun 29th, 2005
 

A transient variable is a variable which may not be serialized 

  Was this answer useful?  Yes

Britto

  • Jul 10th, 2005
 

A transiet variable is a variable that may not be serialised.

  Was this answer useful?  Yes

ramani

  • Jul 22nd, 2005
 

The transient variable is not persist when an object is stored

  Was this answer useful?  Yes

Kishu

  • Jul 27th, 2005
 

 
Answer for "What is a transient variable?" is very informative for technical interviews and for the development side also.

  Was this answer useful?  Yes

Svetlana

  • Aug 18th, 2005
 

Good for people preparing for technical interviews

  Was this answer useful?  Yes

prabu

  • Sep 16th, 2005
 

hai, im prabu ,im studing msc ( software engineering) an intagral coursej2ee concept in lot of examples programs including in the website plz, with j2ee basic questions including in the webpage,

  Was this answer useful?  Yes

test

  • Sep 16th, 2005
 

Mark Wrote: Declaring a Transient Variable  
 
You use the transient keyword to indicate to the Java virtual machine that the indicated variable is not part of the persistent state of the object. Variables that are part of the persistent state of an object must be saved when the object is archived.  
 
At this time, the transient marker is ignored by the Java runtime system. Future releases of the Java system will use the transient marker to implement various object archiving functions.  
 
Like other variable modifiers in the Java system, you use transient in a class or instance variable declaration like this:  
 
This statement declares an integer variable named hobo that is not part of the persistent state of the TransientExample class.

  Was this answer useful?  Yes

Testing

  • Sep 27th, 2005
 

Transient Variable doesnot represent a persistent state of an object

  Was this answer useful?  Yes

Manish Kumar

  • Oct 9th, 2005
 

transient variable is nothing but a variable which is use for transient.

  Was this answer useful?  Yes

g koteshwar rao

  • Oct 10th, 2005
 

A transient variable is a variable that may not be serialized.

  Was this answer useful?  Yes

sushil kumar

  • Oct 18th, 2005
 

Transient variable can't be serialize. For example if a variable is declared as transient in a Serializable class and the class is written to an ObjectStream, the value of the variable can't be written to the stream instead when the class is retrieved from the ObjectStream the value of the variable becomes null.

as an example u can take example of sending some file pointer from one machine to another, we can declare that as transient.

it should also be noted that passworn cannot be declared as transient variable, since its value is to be passed, but should not be disclosed. rather transient variable sends null value.

  Was this answer useful?  Yes

narayana

  • Oct 19th, 2005
 

Mark Wrote: Declaring a Transient Variable  
 
You use the transient keyword to indicate to the Java virtual machine that the indicated variable is not part of the persistent state of the object. Variables that are part of the persistent state of an object must be saved when the object is archived.  
 
At this time, the transient marker is ignored by the Java runtime system. Future releases of the Java system will use the transient marker to implement various object archiving functions.  
 
Like other variable modifiers in the Java system, you use transient in a class or instance variable declaration like this:  
 
This statement declares an integer variable named hobo that is not part of the persistent state of the TransientExample class.

  Was this answer useful?  Yes

SUSHIL KUMAR

  • Nov 15th, 2005
 

Transient variable can't be serialize. For example if a variable is declared as transient in a Serializable class and the class is written to an ObjectStream, the value of the variable can't be written to the stream instead when the class is retrieved from the ObjectStream the value of the variable becomes null.

it should also be noted that password can't be send as a transient variable, since value of transient variable received on other side is NULL.

example of variables to be sent as a transient variable is file pointer.

  Was this answer useful?  Yes

priya

  • Dec 15th, 2005
 

final variable is a constant

  Was this answer useful?  Yes

It is general practice that transient variables are not serialized.

But they can be persisted if we override the ObjectOutputStream.writeobject() method.

  Was this answer useful?  Yes

To create a package is quite simple

->just include a package command as the first statement in java source file

->Any classes declared within that file will belong to the specified package

  Was this answer useful?  Yes

balu

  • Feb 13th, 2006
 

a class inside the class is called innerclass.

  Was this answer useful?  Yes

saurabh shukla

  • Mar 9th, 2006
 

the operator && is known as short circuit operator , in this case the firs expression will be evaluted first and if it is false then second exspression

will not be evaluted , for example

int i=0;

int j=1;

int k=2;

if ( ++j>k &&  ++k>i)

{

}

System.out.println(" i "+i+" j "+j+" k "+k);

above code will print i 0 j 2 k2 , as only ++j> k was evaluted and because it is false jvm made its conclusion that whole exdpression is false.

In case of & ( which is a bitwise operator) both ++j>k and ++k>i will be evaluted

  Was this answer useful?  Yes

SREENIVAS.MAMIDI

  • Mar 24th, 2006
 

A Transient variable is a variable that may not be serialized.

  Was this answer useful?  Yes

vijay Jaiswal

  • Apr 3rd, 2006
 

transient variable is not part of the persistent state of an object.

 

Explaination with example

 

Java's serialization provides an elegant, and easy to use mechanism for making an object's state persistent. While controlling object serialization, we might have a particular object data member that we do not want the serialization mechanism to save.

To turn off serialization on a certain field of an object, we tag that field of the class of our object with the Java's "transient" keyword. This, to low-level parts of the Java virtual machine, is an indication that the transient variable is not part of the persistent state of an object.

First, let's have some backgrounder code with Java's serialization.
Suppose we define a class as:

 
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; 
    } 
}
 

Now we can create an instance of this class and serialize it, and write the serialized object to disk as in:

 
LoggingInfo logInfo = new LoggingInfo("MIKE", "MECHANICS"); 
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} 

To read the object back, we can write

 
try 
{ 
   ObjectInputStream in =new ObjectInputStream( 
                new FileInputStream("logInfo.out")); 
   LoggingInfo logInfo = (LoggingInfo)in.readObject(); 
   System.out.println(logInfo.toString()); 
} 
catch(Exception e) {//deal with exception} 

If we run this code, we notice that the read-back object prints password as "NOT SET". This is exactly the effect we should have expected when we declared the pwd field as transient.

Now, let's see a potential problem that careless treatment of transient fields may cause. Suppose we modify our class definition and provide default values for the transient field, say we write:

 
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 above 
     } 
} 

Now, if we serialize an instance of GuestLoggingInfo, write it to disk, and read it back, we still see that the read-back object prints password as "NOT SET". In effect, the process of reading back (de-serializing) totally ignores the constructor of GuestLoggingInfo. So what happened?

The answer lies in the fact that the initialization code is not called because we are not initializing, in other words, we are not constructing a brand new object, but loading back the persistent state of an object of a class, and assigning that state to another object of the same class. Declaring the pwd field as transient, excludes the data for that field from the persistent state of our object. Then, upon de-serialization, since there is no data preserved for the pwd field, the field gets Java's default value for its type (null for String).

So, if you mark a field of an object as transient, and write that object to disk, expect to have the default value of the type of that field when you de-serialize the object, and not the actual value that the field had before its state was serialized. If a default value (or any meaningful value) is essential for a transient field of a de-serialized object, you have to assign it yourself either directly (if the field is public) or via a setter method.

 

  Was this answer useful?  Yes

Manjunath N

  • Apr 11th, 2006
 

  Collectons store group of objects as a single object, the objects that are stored in a collection are called as elements of that collection.

  Was this answer useful?  Yes

Manjunath N

  • Apr 11th, 2006
 

A class in java must be declared as abstract class if it contains one or more abstract methods, an abstract method can be thought as a prototype method i.e. it contains no body its nothing more than the signature method followed by a semi colon, only way to use the abstarct method is to sub class the abstract class and provide the implementation for all the abstract methods present in that abstract class, abstract class can contain normal method and variables, it is possible that you sub class the abstract class but does not provide the implementation for all the abstract method present in the abstract class in such cases your sub class must also be declared as abstract class.

   interfaces contain only abstract methods, only way to use tha abstract methods is to implement the interface using the key word "implements", when you implement an interface you shud provide the implementation for all the methods present in that interface.

  Was this answer useful?  Yes

Manjunath N

  • Apr 11th, 2006
 

 The acroynm of JAR stands for Java ARchive file it is based on the popular zip format, it is used to collect a group of many files into one, JAR files are meant to be platform independent.

  JAR files comes into handy when working with internet, without JAR files the web browser need to make repeated requests to web server to download all the files that make up an applet. with compressed JAR files with only one request you can download the compressed JAR file that contains all other files.

  Also that all the files in JAR files can be digitally signed for security purposes,

 It can be created programatically or using a comand line utility.

  Was this answer useful?  Yes

Rukmoni

  • Apr 13th, 2006
 

Method Should be of same method name with different arguments.

  Was this answer useful?  Yes

Rukmoni

  • Apr 13th, 2006
 

I think ,

 for the case in which we have to use the attributes and method of the base  class ... its better to use PUBLIC  Modifier for top level class..

 for the case in which we should not use the attributes and method of the base  class ... its better to use Private  Modifier for top level class..

And can also use Protected ... default...(friendly..)

So any type of modifiers can use

  Was this answer useful?  Yes

Rukmoni

  • Apr 13th, 2006
 

Collection is an Interface. It has the follwing types .. that is , List,set..

List: have a collection of objects of particular type. It may have duplicate elements

Set: have a collection of objects of particular type. It may not have duplicate elements

It is in the Util Package

  Was this answer useful?  Yes

Rukmoni

  • Apr 13th, 2006
 

How can we synchronize the array list manualy..?

  Was this answer useful?  Yes

Raja Muthusamy

  • May 9th, 2006
 

Collection APIs are predefined Classes and Interfaces, those can be used to manipulate collection of data.

  Was this answer useful?  Yes

nimesh

  • May 26th, 2006
 

what do u mean by serialization can u pls explain in detail ?

  Was this answer useful?  Yes

Arun

  • Apr 4th, 2007
 

Somebody pls tell what is real use of transient variables, rather than the definition.
Some real example is most welcome...

Thanks
Arun

  Was this answer useful?  Yes

thallamudi

  • May 24th, 2007
 

Transient variable is used when we want  to hide certain info while sending it to server.For eg confidential data related to  banking applications,say ur credit card num.

  Was this answer useful?  Yes

pavang

  • Jun 8th, 2007
 

true, false, and null might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs.

  Was this answer useful?  Yes

Jeevan

  • Sep 19th, 2007
 

Hi,

The transient keyword signifies that the variables should not be stored when objects are serialized.

  Was this answer useful?  Yes

nileshchauhan1984

  • Oct 1st, 2007
 

Transient Variable is a variable which cannot be Serialized. If we don't want to save the state of any variable during Serialization, we use transient keyword to protect variable from Serialization.

  Was this answer useful?  Yes

Sai Chakrapani. Sunkara

  • Oct 7th, 2007
 

The presence of the transient modifier is one means to tell the application not to save the variable.

  Was this answer useful?  Yes

sampra

  • Feb 13th, 2008
 

transient varibale are those varibale which we cant serlized suppose if we dont want share some passrord or account pin no then declare those variable as transient

  Was this answer useful?  Yes

subirjolly

  • Jun 9th, 2008
 

I think that in this case 3 processes will be started because we are starting individual programs. If we are starting 2 or more tabs in IE then we are starting threads. Notepad is SDI, so it will not have multiple threads.

So, I will stick to the processes and not the threads.

  Was this answer useful?  Yes

sivagopal

  • Jun 8th, 2009
 

A variable that won't be allowed for object serialisation. so that the state
of the value will always be defaulted after the deserialisation.
For example a variable x's value is set to 9, it's default value is '0' say,
when the object has been serialized having x's value 9, after deserialisation
will be defaulted to '0'


Use: In Java, serialization of an object allowed only when all the underlying
objects of the object that is currently under serialization contains has a
relationship, will not be allowed, some times the developer has no choice of
implementing serializable marker interface on some classes as the classes might
have been arrived from a third party and the developer has no control over them,
but the developer needs to serialize the object's state, then the developer has
the choice of marking the objects that not serializable as transient.

juneja

  • Dec 7th, 2009
 

A transient variable is a variable that can not be serialized.

For Example,
class Employee implements Serializable {
String name;
int age;
transient long salary;
}

When executing this program, the employees name and age will be stored into a an outputstream but the salary will not be stored.

Finalize method is used to release the resource before the garbage collection.
Finalalisation does not do any clean up operation, that just run finalize method of the object. So if you want release resource just write in finalize method.

  Was this answer useful?  Yes

sivakumar

  • Feb 11th, 2010
 

It is a variable which we can't serialize.

  Was this answer useful?  Yes

manokarand

  • Jan 6th, 2011
 

Yes. we can store transient variable value.


private String transient -- do not store persistent state.
private static String transient -- can store persistent state.
private final String transient -- can store persistent state.
private static final String transient -- can store persistent state.

  Was this answer useful?  Yes

vijay

  • Sep 12th, 2011
 

A transient variable is a variable that may not be serialised.

  Was this answer useful?  Yes

raisha

  • May 6th, 2013
 

transient is a keyword....If a variable is declared as a Transient ,we cant serialize that variable..
The important points about Transient are:
1) Transient keyword can only be applied to fields or member variable. Applying it to method or local variable is compilation error.
2) Another important point is that you can declare an variable static and transient at same time and java compiler doesn't complain but doing that doesn't make any sense because transient is to instruct "do not save this field" and static variables are not saved anyway during serialization.
3) In similar way you can apply transient and final keyword together to a variable compiler will not complain but you will face another problem of reinitializing a final variable during deserialization.
4) Transient variable in java is not persisted or saved when an object gets serialized.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions