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

  • SEARCH
  • Home
  • 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 8818355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:05:17+00:00 2026-06-14T05:05:17+00:00

As I understand the case like: class Case1{ private Object object; public synchronized Object

  • 0

As I understand the case like:

class Case1{
        private Object object;
        public synchronized Object getObject() {
            return object;
        }
        public synchronized void setObject(Object object) {
            this.object = object;
        }
    }

would be considered like deprecated in comparison with the:

class Case2{
    private AtomicReference<Object> object = new AtomicReference<Object>();
    public Object getObject() {
        return object.get();
    }
    public void setObject(Object object) {
        this.object.set(object);
    }
}

Am i right?

  • 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-06-14T05:05:18+00:00Added an answer on June 14, 2026 at 5:05 am

    In both cases, the operation within the getter and the setter is atomic (reference assignment). So a more efficient (at least in terms of readability) idiom would be:

    class Case3 {
        private volatile Object object = new Object();
        public Object getObject() {
            return object;
        }
        public void setObject(Object object) {
            this.object = object;
        }
    }
    

    As for your actual question, AtomicReference provides a few simple atomic operations which would make it a good choice over volatile if you need them. And synchronized makes the whole block atomic, which enables you to make even more complex operations in an atomic way.

    In other words, volatile, AtomicReference and synchronized all have their specificities and are not equivalent.

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

Sidebar

Related Questions

If I have an abstract class like this: public abstract class Item { private
Suppose a construct like this: class Interface { public: template <typename T> virtual void
I understand I can create an enum like this: public enum MyEnum { ONE(1),
EDIT: I know in this case, if it were an actual class i would
Can someone help me understand how to write this case statement properly its not
I have a validation class which I would like to use to check all
This is more a design question than anything else... I really like Scala's case
I understand from bunch of other Stackoverflow threads (like this ) that Template arguments
While developing a test case to understand serialization, I've run into what looks like
public class MainClass { private static final int size = 5; private ExecutorService prodExec

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.