aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static, transient and volatile variables in Serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static, transient and volatile variables in Serialization" Watch "static, transient and volatile variables in Serialization" New topic
Author

static, transient and volatile variables in Serialization

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi, I am sure that transient variable(s) do not participate in Serialization. What about volatile and static variable(s)..Do they participate in Serialization ?


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16

Only volatile should be serialized, I think.
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
Static variables are not serialized.
But they do take default initialized value.

Examine the following code:




The output is 1 0 9
If static variable is not initialized on declaration, then the output is 1 0 0


OCPJP6
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

by the serialization has been removed from SCJP 6.0,me too didn't find any question on same topic.


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Sven Mathijssen
Greenhorn

Joined: Aug 29, 2010
Posts: 19
Arjun Srivastava wrote:by the serialization has been removed from SCJP 6.0,me too didn't find any question on same topic.


Where did you get this information? As far as I know, serialization IS on the SCJP 6 exam.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
much to our dismay, serialization was recently removed from the SCJP / OCP objectives

we still occasionally hear reports of prometric centers using older versions of the exams with serialization questions included, so if your center is fairly remote, beware.

hth,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Bert Bates wrote:much to our dismay, serialization was recently removed from the SCJP / OCP objectives

we still occasionally hear reports of prometric centers using older versions of the exams with serialization questions included, so if your center is fairly remote, beware.

hth,

Bert
Are you saying that exam questions depends on the testing center ?
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi Trivikram, I clubbed your 2 files into 1 file as given below. Now, Do you know why I am surprisingly getting output as 1 0 3 instead 1 0 9
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
Harikrishna Gorrepati wrote:
Hi Trivikram, I clubbed your 2 files into 1 file as given below. Now, Do you know why I am surprisingly getting output as 1 0 3 instead 1 0 9


Static variables are class variables, which are shared between the instances of the class.
When you read the serialized object, the static variable z is given already existing value, which is 3 - assigned by the object which was serialized.
That is the reason I'd split the code into two files to remove confusion.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Yes, it's an imperfect world, and not all the world-wide Prometric centers roll out exactly the same sets of questions at exactly the same time.

I understand that Oracle is somewhere in the process of transitioning away from Prometric to a different test center provider, but I know nothing about who or when.

hth,

Bert
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Trivikram Kamat wrote:
Harikrishna Gorrepati wrote:
Hi Trivikram, I clubbed your 2 files into 1 file as given below. Now, Do you know why I am surprisingly getting output as 1 0 3 instead 1 0 9


Static variables are class variables, which are shared between the instances of the class.
When you read the serialized object, the static variable z is given already existing value, which is 3 - assigned by the object which was serialized.
That is the reason I'd split the code into two files to remove confusion.
Hi Trivikram, I am sure you explained but I am not able to catch you. Could you please elaborate further on this. All we trying here is, separating last 3 lines into separate file or keep the complete code in single file. How it should affect the output because of these changes.
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
Harikrishna Gorrepati wrote:
I am sure you explained but I am not able to catch you.


The static instance variables are class variables and have class scope. While running a program, they're initialized only once when the first class object is created.
The value of the static variable persists even if the object is GCed.
If another object is created in the program, it'll take existing value of static variable.

See the below code:
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi Trivikram, Sorry for bothering you. I think I did not ask the question in the right way. Why we are getting different output ( 3 Vs 9 ) when we separated the piece of code..I understand the concept of static(Class) variables.
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
Harikrishna Gorrepati wrote:
Why we are getting different output ( 3 Vs 9 ) when we separated the piece of code..I understand the concept of static(Class) variables


Ok, let me explain this way.

When an object is deserialized, that static variables get the default initialized value only if that's the first instance of the class.
This happens when you separate the code into two files, where obj is the first instance of the class SpecialSerial in ReadSpecialSerial. So, the output is 9

If instances of the class already exist/existed in the program before, then the static variable of the deserialized object gets the existing value set by the previous objects.
That is why, you get output 3 when you combine both files in class SpecialSerial.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static, transient and volatile variables in Serialization