Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 887109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:10:36+00:00 2026-05-15T13:10:36+00:00

What happens if i’ll try to serialize an attribute which is static? thanks

  • 0

What happens if i’ll try to serialize an attribute which is static?

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:10:36+00:00Added an answer on May 15, 2026 at 1:10 pm

    From this article:

    Tip 1: Handling Static Variables

    Java classes often hold some
    globally relevant value in a static
    class variable. We won’t enter into
    the long history of the debate over
    the propriety of global variables –
    let’s just say that programmers
    continue to find them useful and the
    alternatives suggested by purists
    aren’t always practical.

    For static variables that are
    initialized when declared,
    serialization doesn’t present any
    special problems. The first time the
    class is used, the variable in
    question will be set to the correct
    value.

    Some statics can’t be initialized this
    way. They may, for instance, be set by
    a human during the running time of the
    program. Let’s say we have a static
    variable that turns on debugging
    output in a class. This variable can
    be set on a server by sending it some
    message, perhaps from a monitor
    program. We’ll also imagine that when
    the server gets this message, the
    operator wants debugging turned on in
    all subsequent uses of the class in
    the clients that are connected to that
    server.

    The programmer is now faced with a
    difficulty. When the class in question
    arrives at the client, the static
    variable’s value doesn’t come with it.
    However, it contains the default
    static state that’s set when the
    class’s no-argument constructor is
    called by writeObject(). How can the
    client programs receive the new
    correct value?

    The programmer could create another
    message type and transmit that to the
    client; however, this requires a
    proliferation of message types,
    marring the simplicity that the use of
    serialization can achieve in
    messaging. The solution we’ve come up
    with is for the class that needs the
    static transmitted to include a
    “static transporter” inner class. This
    class knows about all the static
    variables in its outer class that must
    be set. It contains a member variable
    for each static variable that must be
    serialized. StaticTransporter copies
    the statics into its member variables
    in the writeObject() method of the
    class. The readObject() method
    “unwraps” this bundle and transmits
    the server’s settings for the static
    variables to the client. Since it’s an
    inner class, it’ll be able to write to
    the outer class’s static variables,
    regardless of the level of privacy
    with which they were declared.

    And from another article:

    Static or transient data

    However, this “ease” is not true in
    all cases. As we shall see,
    serialization is not so easily applied
    to classes with static or transient
    data members. Only data associated
    with a specific instance of a class is
    serialized, therefore static data,
    that is, data associated with a class
    as opposed to an instance, is not
    serialized automatically. To serialize
    data stored in a static variable one
    must provide class-specific
    serialization.

    Similarly, some classes may define
    data members to use as scratch
    variables. Serializing these data
    members may be unnecessary. Some
    examples of transient data include
    runtime statistics or hash table
    mapping references. These data should
    be marked with the transient modifier
    to avoid serialization. Transient, by
    definition, is used to designate data
    members that the programmer does not
    want or need to be serialized. See
    Java in a Nutshell, page 174: mouse
    position, preferred size, file handles
    (machine specific (native code)).

    When writing code if something is
    declared transient, then this triggers
    (to programmer) necessity of the
    posibility of special code for
    serialization later.

    To serialize an object, you create
    some sort of OutputStream object and
    then wrap it inside an
    ObjectOutputStream object. At this
    point you only need to call
    writeObject() and your object is
    magically serialized and sent to the
    OutputStream. To reverse the process,
    you wrap an InputStream inside an
    ObjectInputStream and call
    readObject(). What comes back is, as
    usual, a handle to an upcast Object,
    so you must downcast to set things
    straight. If you need to dynamically
    query the type of the object, you can
    use the getClass method. Specifically
    dk.getClass.getName() returns the name
    of the class that dk is an instance
    of. I.e., this asks the object for the
    name of its corresponding class
    object. (Hmmm, True, but what about
    syntax? I still need to know what it
    is to declare it…too bad) (C++ can
    do this in one operation (dynamic_cast
    (gives null if wrong type)), java can
    use instanceof operator to check if it
    is what I think (see Core Java, Ch5
    Inheritence, Casting section)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What happens if you try to add an item to a Collection that already
What happens when you serialize data? Seriously, I'm being bothered about what's the use
What happens when you try to add a variable into APC and the APC
This happens when I try to deploy a Sharepoint WebPart solution. Is there a
What happens in memory when we pass a value type - which has been
This happens when I try to run rinari-cap: ~/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:29:in `gem_original_require': no such file to
What happens when a thread is put to sleep by other thread, possible by
What happens in the following code? Does the synchronization work? This is an interview
What happens in the following example? struct B { }; struct D1 : B
What happens when an application pool is recycled in IIS 7? Does IIS abruptly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.