GeekInterview.com
Series: Subject: Topic:
Question: 2110 of 2110

what is a transient variable

A transient variable is a variable that may not be serialized.
Asked by: Interview Candidate | Asked on: Sep 4th, 2005

Editorial / Best Answer

Answered by: sivagopal

View all answers by sivagopal

Member Since Jun-2009 | Answered On : 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

Answered On : May 23rd, 2005

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

  
Login to rate this answer.
Mark

Answered On : 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.

  
Login to rate this answer.
Roko

Answered On : 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

  
Login to rate this answer.
vijay

Answered On : 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.  

  
Login to rate this answer.
preeti

Answered On : Jun 29th, 2005

A transient variable is a variable which may not be serialized 

  
Login to rate this answer.
Britto

Answered On : Jul 10th, 2005

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

  
Login to rate this answer.
ramani

Answered On : Jul 22nd, 2005

The transient variable is not persist when an object is stored

  
Login to rate this answer.
Kishu

Answered On : Jul 27th, 2005

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

  
Login to rate this answer.
Svetlana

Answered On : Aug 18th, 2005

Good for people preparing for technical interviews

  
Login to rate this answer.
sai_iegemp

Answered On : Sep 14th, 2005

View all answers by sai_iegemp

Transient variable should not serialize

  
Login to rate this answer.
prabu

Answered On : 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,

  
Login to rate this answer.
test

Answered On : 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.

  
Login to rate this answer.
Testing

Answered On : Sep 27th, 2005

Transient Variable doesnot represent a persistent state of an object

  
Login to rate this answer.
Manish Kumar

Answered On : Oct 9th, 2005

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

  
Login to rate this answer.
g koteshwar rao

Answered On : Oct 10th, 2005

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

  
Login to rate this answer.
sushil kumar

Answered On : 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.

  
Login to rate this answer.
narayana

Answered On : 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.

  
Login to rate this answer.
SUSHIL KUMAR

Answered On : 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.

  
Login to rate this answer.
priya

Answered On : Dec 15th, 2005

final variable is a constant

  
Login to rate this answer.
Mukesh Syal

Answered On : Jan 9th, 2006

View all answers by Mukesh Syal

It is general practice that transient variables are not serialized.But they can be persisted if we override the ObjectOutputStream.writeobject() method.

  
Login to rate this answer.
cyber_akhlesh

Answered On : Jan 13th, 2006

View all answers by cyber_akhlesh

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

  
Login to rate this answer.
cyber_akhlesh

Answered On : Jan 13th, 2006

View all answers by cyber_akhlesh

A class declared as final cannot be inherited by its subclasses .

  
Login to rate this answer.
balu

Answered On : Feb 13th, 2006

a class inside the class is called innerclass.

  
Login to rate this answer.
saurabh shukla

Answered On : 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 exspressionwill not be evaluted , for exampleint 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

  
Login to rate this answer.
SREENIVAS.MAMIDI

Answered On : Mar 24th, 2006

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

  
Login to rate this answer.
vijay Jaiswal

Answered On : 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.  

  
Login to rate this answer.
Manjunath N

Answered On : 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.

  
Login to rate this answer.
Manjunath N

Answered On : 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.

  
Login to rate this answer.
Manjunath N

Answered On : 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.

  
Login to rate this answer.
Rukmoni

Answered On : Apr 13th, 2006

Method Should be of same method name with different arguments.

  
Login to rate this answer.
Rukmoni

Answered On : 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

  
Login to rate this answer.
Rukmoni

Answered On : 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 elementsSet: have a collection of objects of particular type. It may not have duplicate elementsIt is in the Util Package

  
Login to rate this answer.
Rukmoni

Answered On : Apr 13th, 2006

How can we synchronize the array list manualy..?

  
Login to rate this answer.
Raja Muthusamy

Answered On : May 9th, 2006

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

  
Login to rate this answer.
nimesh

Answered On : May 26th, 2006

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

  
Login to rate this answer.
vivek_asct

Answered On : Feb 20th, 2007

View all answers by vivek_asct

null is not a keyword.Its a value , that can be asigned to any Object or primitive data type.

  
Login to rate this answer.
vivek_asct

Answered On : Feb 20th, 2007

View all answers by vivek_asct

Collection api is set of classess and interfaces , that is used to handle mass data as a single entity

  
Login to rate this answer.
Arun

Answered On : 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

  
Login to rate this answer.
vipin.hbti.in

Answered On : Apr 26th, 2007

View all answers by vipin.hbti.in

Transient varibale can not be serialize,ie it can not parsist its state..

  
Login to rate this answer.
thallamudi

Answered On : May 24th, 2007

View all answers by thallamudi

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.

  
Login to rate this answer.
pavang

Answered On : Jun 8th, 2007

View all answers by pavang

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

  
Login to rate this answer.
Jeevan

Answered On : Sep 19th, 2007

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

  
Login to rate this answer.
crsindhu.27

Answered On : Oct 1st, 2007

View all answers by crsindhu.27

It returns null

  
Login to rate this answer.
nileshchauhan1984

Answered On : 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.

  
Login to rate this answer.
Sai Chakrapani. Sunkara

Answered On : Oct 7th, 2007

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

  
Login to rate this answer.

Null is one of the keyword in java.true, false, and null might seem like keywords, but they are actually literals.(Read from Sun site).

  
Login to rate this answer.
sampra

Answered On : Feb 13th, 2008

View all answers by sampra

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

  
Login to rate this answer.
subirjolly

Answered On : Jun 9th, 2008

View all answers by subirjolly

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.

  
Login to rate this answer.
sivagopal

Answered On : Jun 8th, 2009

View all answers by sivagopal

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.

Yes  1 User has rated as useful.
  
Login to rate this answer.
juneja

Answered On : Dec 7th, 2009

View all answers by juneja

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.

Yes  1 User has rated as useful.
  
Login to rate this answer.
elayarajaselva

Answered On : Jan 10th, 2010

View all answers by elayarajaselva

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.

  
Login to rate this answer.
sivakumar

Answered On : Feb 11th, 2010

It is a variable which we can't serialize.

  
Login to rate this answer.

Transient is a keyword and which is not serializeable

  
Login to rate this answer.
manokarand

Answered On : Jan 6th, 2011

View all answers by manokarand

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.

  
Login to rate this answer.
vijay

Answered On : Sep 12th, 2011

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

  
Login to rate this answer.
raisha

Answered On : 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.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.