Synchronization is required in getters and setters


snow leopard

Probably a very stupid question. Just wanted to confirm my understanding.

class Test
{
       private volatile String id;

       public void setID(String id)
       {
             this.id = id;
       }

       public String getID()
       {
             return id;
       }
}

It can be said that objects of more than one class can be accessed by multiple threads. My understanding is that in the case of simple getter and setter methods like above (with simple initialization), I don't need to make these methods synchronized, right?
I guess volatile is needed, otherwise the value of the id might be out of date, otherwise in a different thread.
So if we don't sync these methods, who can see any problems?

TJ crowd

My understanding is that in the case of simple getter and setter methods like above (with simple initialization), I don't need to make these methods synchronized, right?

Correct, because the JVM treats what they get and set (object references) atomically.

The answer is "no, you need to sync to do that", if you're using the or , you don't have it flagged .longdoublevolatile

Section 17.7 of the JLS covers both :

17.7. Non-atomic handling of diatomic and long atoms

For the purposes of the Java programming language memory model, a single write to a nonvolatile long or double value is treated as two separate writes: one write to each of the 32-bit halves. This can lead to a situation where a thread sees the first 32 bits of a 64-bit value from one write and the last 32 bits from another write.

Writes and reads of volatile long and double values ​​are always atomic.

Writes and reads of references are always atomic, whether they are implemented as 32-bit or 64-bit values.

Related


Synchronization is required in getters and setters

snow leopard Probably a very stupid question. Just wanted to confirm my understanding. class Test { private volatile String id; public void setID(String id) { this.id = id; } public String getID() {

Synchronization is required in getters and setters

snow leopard Probably a very stupid question. Just wanted to confirm my understanding. class Test { private volatile String id; public void setID(String id) { this.id = id; } public String getID() {

Setters and Getters

anonymous My problem is that I simply don't know what code to use to pass the value from the getX method to the other class main method. package hangman; Hangman of public class { private int triesLimit; private String word; public void setTriesLimit(int tri

Setters and Getters

anonymous My problem is that I simply don't know what code to use to pass the value from the getX method to the other class main method. package hangman; Hangman of public class { private int triesLimit; private String word; public void setTriesLimit(int tri

Are both getters and setters required in JSF's managed beans?

jj88: I have a JSF component which is initialized from a managed bean's getter getProperty(). Also must there be a setter setProperty()in that managed bean ? McDowell: it depends on. Certain property bindings must be readable and writable. For example, if you

Are both getters and setters required in JSF's managed beans?

jj88: I have a JSF component which is initialized from a managed bean's getter getProperty(). Also must there be a setter setProperty()in that managed bean ? McDowell: it depends on. Certain property bindings must be readable and writable. For example, if you

Are both getters and setters required in JSF's managed beans?

jj88: I have a JSF component which is initialized from a managed bean's getter getProperty(). Also must there be a setter setProperty()in that managed bean ? McDowell: This depends on. Certain property bindings must be readable and writable. For example, if yo

Getters and Setters not working properly

King Bogle: I need help, I'm doing my homework and getters and getters are not working. So im using an action listener and when add amount is clicked it should add the entered amount to the deposit amount and it does so, but when i call getSavingBalance(); the

Setters and getters written in Java

Toyota: I have a problem with setters and getters in Java when using composition instead of inheritance. This question came when I was working on my college assignments. Say I have two classes: Car and Battery. The battery has 3 variables (var1, var2, var3) wi

Generate getters and setters in NetBeans

star: I need to know how to get NetBeans to generate getters and setters using shortcuts. Stefano Sanfilippo : Put the cursor inside the class, then press ALT+ Insand Getters and Settersselect from the context menu.

Using @property with getters and setters

Zaur Nasibov : This is a purely Python specific design question: class MyClass(object): ... def get_my_attr(self): ... def set_my_attr(self, value): ... and class MyClass(object): ... @property def my_attr(self

Should getters and setters be synchronized?

DD。: private double value; public synchronized void setValue(double value) { this.value = value; } public double getValue() { return this.value; } In the above example, does it make sense to synchronize the getter? Konrad Reiche: I think it's better

Array getters and setters

User 837324: I have some questions about getters and setters for arrays. Suppose we have a class like this that creates a private copy of the array in its constructor: import java.util.Arrays; public class Foo { private int[] array; public Foo(int[]

Getters and Setters in Subclasses

Joe Butler: I'm just learning inheritance in programming and I'm wondering if you should write overridden getters and setters for instance variables in each subclass, or just use inherited classes from abstract parent classes. Is it bad code to write getters a

Interface with getters and setters in Go

user2727195: I'm new to the language and have a problem using getters and setters to specify the interface of a struct from different files. resource src / github.com / user / interfaces package interfaces type IFoo interface { Name() string SetName(n

no body getters, some setters

colmde: I have a property that is currently automatic. public string MyProperty { get; set; } However, I now need it to do something on every change, so I want to add logic to the setter. So I want to do something like this: public string MyProperty { get

Getters Setters with user input

Sheikh Rahman: Is it good practice to use getters and setters with user input? I looked for some examples but couldn't find any. While the code below works, is it a good practice? I noticed that if I use the code below, I can't use the constructor. thank you f

Getters and Setters in pure C?

elusive Can I use getters and setters in pure C without extern variables? Lundin First of all, don't listen to anyone saying "there is no object orientation in x language" because they really don't understand that OO is a completely different approach to progr

Caching in getters/setters?

novice programmer If I have a getter like this: export class foo{ aaa: number; bbb: number; ccc: number; constructor(){ //whatever } get getHeight(f: number){ return (f + 16) * Math.ceil(Math.round(Math.tan(aaa) + bbb/

Redundant code in getters and setters

jcai I have a class that needs a method to NotifyPropertyChangedchange any property . What I see in the example is this: private string property1_; public string Property1 { get { return property1_; } set { property1_ = value; NotifyPropertyChanged();

getters/setters on modules in TypeScript

Daniel Sedleczek I am using AMD modules (compiler flag "--module amd") in a TypeScript project. While I can easily use getters/setters on classes, I would like to do the same on modules, but export get abc() : string { return "abc"; } return the goods err

Getters and Setters of ArrayList in Java

user5340035: How would I use Java's ArrayList's getter and setter methods? This is what I have. I think I'm using getters correctly, but I don't know how to use setters for ArrayList. private ArrayList<String> stringlist = new ArrayList<String>(); public Array

Getters and Setters in pure C?

elusive Can I use getters and setters in pure C without extern variables? Lundin First of all, don't listen to anyone saying "there is no object orientation in x language" because they really don't understand that OO is a completely different approach to progr

Bindings and Getters and Setters

boogie Why does public string name {get; set;}it work when binding, but public string name;not? Why do you need a getter when they both return the same string? mm 8 Because you can only bind to public properties in WPF . The following are fields instead of pro