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 650709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:04:57+00:00 2026-05-13T22:04:57+00:00

The Effective Java says that readResolve works only if all fields are transient .

  • 0

The Effective Java says that readResolve works only if all fields are transient.
Isn’t this a bug? Why would the Java creators do a such thing?

— update

Sorry, I mean the More Effective Java, see slide 30.

  • 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-13T22:04:58+00:00Added an answer on May 13, 2026 at 10:04 pm

    For completeness, slide 29 is:

    Item 77: Pop Quiz: Is This Class a Singleton?

    public class Elvis implements Serializable {
        public static final Elvis INSTANCE = new Elvis();
        private Elvis() { }
        private final String[] favoriteSongs =
            { "Hound Dog", "Heartbreak Hotel" };
        public void printFavorites() {
            System.out.println(Arrays.toString(favoriteSongs));
        }
        private Object readResolve() {
            return INSTANCE;
        }
    }
    

    slide 30 is:

    Answer: Unfortunately Not

    The first edition oversold the power of readResolve

    > Elvis has a nontransient field
    (favoriteSongs)
    > Cleverly
    crafted attack can save reference to
    deserialized
       Elvis instance when
    this field is deserialized

    • See ElvisStealer for details (Item 77)

    > readResolve works only if
    all fields are transient

    Item 77 is:

    Item 77: For instance control, prefer enum types to readResolve

    …

    If the Elvis class is made to
    implement Serializable, the following
    readResolve method suffices to
    guarantee the singleton property:

    // readResolve for instance control - you can do better!
    private Object readResolve() {
      // Return the one true Elvis and let the garbage collector
      // take care of the Elvis impersonator.
      return INSTANCE;
    }
    

    This method ignores the deserialized
    object, returning the distinguished
    Elvis instance that was created when
    the class was initialized. Therefore,
    the serialized form of an Elvis
    instance need not contain any real
    data; all instance fields should be
    declared transient. In fact, if you
    depend on readResolve for instance
    control, all instance fields with
    object reference types must be
    declared transient.
    Otherwise, it is
    possible for a determined attacker to
    secure a reference to the deserialized
    object before its readResolve method
    is run, using a technique that is
    vaguely similar to the MutablePeriod
    attack in Item 76.

    The attack is a bit complicated, but
    the underlying idea is simple. If a
    singleton contains a nontransient
    object reference field, the contents
    of this field will be deserialized
    before the singleton’s readResolve
    method is run. This allows a carefully
    crafted stream to “steal” a reference
    to the originally deserialized
    singleton at the time the contents of
    the object reference field are
    deserialized.

    It’s not a case of only working if all the fields are transient, it’s that it’s only safe if all Object references are transient. The slide you reference goes further and says is any fields are non-transient an attacker can grab a reference to the deserialized Object.

    As to why: one can argue that Josh Bloch’s use of readResolve() for a singleton was an unintended use of the API, one the creators didn’t envision when they made it. You could also argue it’s simply an unforeseen consequence. I don’t think however it was a deliberate weakness.

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

Sidebar

Ask A Question

Stats

  • Questions 350k
  • Answers 350k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Used ICallbackEventHandler May 14, 2026 at 6:59 am
  • Editorial Team
    Editorial Team added an answer Turns out that retaining focus on the field from which… May 14, 2026 at 6:59 am
  • Editorial Team
    Editorial Team added an answer I would define a data class (TraceEntry) that encapsulates a… May 14, 2026 at 6:59 am

Related Questions

Question: Is exception handling in Java actually slow? Conventional wisdom, as well as a
So, my problem is the following: I have a mail client that I wrote
I want to maintain state in my Perl web app. How can I do
In the hidden features of java question, someone mentions that final initialization can be
The book Effective Java and other sources provide a pretty good explanation on how

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.