File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static, transient and volatile variables in Serialization

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Stephan van Hulst
Bartender
Pie
Posts: 4846
34
Chrome Netbeans IDE Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only volatile should be serialized, I think.
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by the serialization has been removed from SCJP 6.0,me too didn't find any question on same topic.
 
Sven Mathijssen
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic