• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reference variables

 
Ranch Hand
Posts: 40
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi.. I just read these 2 sentence in SCJP Java 6 but i didnt understand what exactly they mean. Please explain

1. If a reference variable is marked final, data within it can be modified, but the reference variable cannot be changed. (what cannot be changed?)

2. If you mark an instance variable as transient, JVM skips this variable while serializing the object containing it. (why does it serialize objects?)
 
Ranch Hand
Posts: 35
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, if an object is marked final you can never change what that object to new Object or make it refer to another instance of it. It is final...but if the object that is marked final has members like ints and Strings that aren't marked final they can be changed "in" the final object reference.
I hope that is clear in the example below.



For the second issue. It's the programmer that does the serialization. It comes in a later chapter so for now just understand that if something is being serialized transient variables are not serialized.

I hope that helps.
 
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello Anjali
Welcome to java ranch...

1) Correct statement is "The data within the object can be modified, but the reference variable cannot be changed."
In java, reference variables are used to refer to the objects created on the heap (Analogous to pointers in c/c++)
The statement mean if you marked the variable the content of variable cant be changed i.e it refer to the same object once initialized..
How ever the state of the object can be changed/accessed by calling its methods ...

2)If you mark an instance variable as transient, JVM skips this variable while serializing the object containing it.
Serialization is a process of saving objects,While doing that process sometimes you may need to skip some variables, remember this for now...

Note that serialization concepts have been removed from exam's objectives,
If you are interested you can refer other books (I think old edition of K&B still contains serialization concepts)...

HTH
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anjali,

As the word itself says - "Final" means the reference is final. for example -

Lets say we have a class Bus


For Serialization, you may refer to this article -
Basics of Serialization



 
Anjali Vaidya
Ranch Hand
Posts: 40
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Darren- Thanks for the example. It was a perfect example to clear my doubts

Hareendra- Thanks for telling me that serialization in detail has been removed from the exam objectives. I find that concept a bit tougher

Sunny- Thank you, will surely look into the link
 
reply
    Bookmark Topic Watch Topic
  • New Topic