0 votes
in Java/J2EE by
Can transient variables be declared as 'final' or 'static'?
   

1 Answer

0 votes
by Hot Users (2.4k points)
Before Transient you should know about Serialization

Serialization is process of making the object's state persistent which means that the state of the object is converted into a stream of bytes and will be stored in a file. we can use deserialization to transform to object's state from bytes.we should implement the serializable interface with our class.

transient keyword is used in serialization. If you define any data member as transient, it will not be serialized.


Let's take an example, I have declared a class as Student, it has three data members id, name and age. If you serialize the object, all the values will be serialized but I don't want to serialize one value, e.g. age then we can declare the age data member as transient.

Syntax :

class student {

public transient int id;

}

The id value will be 0 when you serialize the student object and store it in a .ser file.

Answer is Yes, you can define as static and final. The compiler won't complain.

final transient: You can use it if you are declaring non-static constants [1] in your class.
public transient final double PI = 3.14159;
However, if you define some arbitrary objects as final transient for e.g.
public transient final Map<String,Integer> dictionary = loadDictionary();
Then, during de-serialization, the object will have these members as null. Also, it is impossible to reinitialize these values elsewhere because these members are declared as final.

static transient: This could be useful, if you are using a non-standard serializer that also serializes static variables. Otherwise, this is a superfluous statement and you could just use "static".

static final transient: Just take the union of point 1 and 2 above and it will hold for this case.

Static variables are not serialized ( atleast with standard serializers ) so there is no sense marking a variable static as well as transient.

Marking final variable as transient will disable its participation in serialization or deserialization. Final has no relevance with transient in terms of serialization.

Related questions

0 votes
1 answer 118 views
0 votes
1 answer 147 views
0 votes
1 answer 134 views
Welcome to Find4Answers.com

Where you can Ask Questions, Find Answers Or Receive Answers from other members of the community And Share in Social networking sites like facebook, linkedin, twitter.
3cx phone system bandar bola benches boiler business bóg chwilówka przez internet car car lease deals car leasing uk chwilówka przez internet zapewne company convey convey law complaints convey law reviews convey law service conveylaw cornwall cosmetic dentist data data recovery maidenhead data recovery nottingham deals deepika padukone deepika padukone hot design development dot net programming dzieje szybkie chwilówki electrician electrician manchester electricians emergency farm filmy bez limitu filmy bez limitów filmy online finlock finlock solutions general genral graduate jobs in london graduate jobs london gutters hard drive recovery heap http: www.seo-assist.in infrared sauna infrared sauna saunas inline skating installation jabong coupon java judi bola laptop recycling law law firm in leeds lease leasing led lights led bulbs leeds life insurance quotes log london manchester movies museums and art myntra coupon nikogo szybka chwilówka outsource link building personal porcelain veneers ramię szybkie chwilówki recovery seo seo agencies seo company seo company london seo services services solutions stair lifts steel synchronization szybka chwilówka przykład szybka chwilówka żaden szybkie chwilówki lekko thread tunnel lighting tymczasem chwilówki przez internet upvc windows manchester viagra kamagra videos vinyl flooring suppliers wait web design services london web development company window repairs london windows yebhi coupon zobaczyć szybka chwilówka
...