Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.496 :

Posted: Sat Jul 23, 2011 12:41 pm
by ETS User
I didn't understand the concept of object wrapping.... infact the whole question and answer... can somebody explain me this?

Re: About Question com.enthuware.ets.scjp.v6.2.496 :

Posted: Thu Oct 24, 2013 9:59 pm
by canus27
A programmer is using the following class for wrapping objects and passing it around to multiple threads. Which of the given statements regarding this class are correct?

public class DataObectWrapper
{
private Object obj;

public DataObectWrapper(Object pObj){ obj = pObj; }

public Object getObject() { return obj; }
}
This question is what the above user (2 yrs ago) was referring to..an explanation would be nice:)

Re: About Question com.enthuware.ets.scjp.v6.2.496 :

Posted: Fri Oct 25, 2013 5:00 am
by admin
Hi Canus,
There is a detailed explanation already with the question. Could you please be a little more specific about which part you don't understand so that we can help?
-Paul.

Re: About Question com.enthuware.ets.scjp.v6.2.496 :

Posted: Wed Oct 30, 2013 10:20 pm
by canus27
I understand how this works and the flow of the code. How is this wrapping an object? Looks to me (in my very limited experience) that this is a class declaration and constructor. Am I incorrect?

Re: About Question com.enthuware.ets.scjp.v6.2.496 :

Posted: Thu Oct 31, 2013 7:14 am
by admin
The class is not really doing anything other than getting and setting the internal object referred to by "obj". In that sense, it just wrapping the object referred to by obj. There is nothing much to it.

HTH,
Paul.

Re: About Question com.enthuware.ets.scjp.v6.2.496 :

Posted: Tue Sep 20, 2022 7:21 pm
by cjgiron
Hi Admin,

Just wanted to clarify my understanding of thread-safety in terms of objects. So if a class A has methods that modify instance or class variables of class A objects, that automatically means class A is not thread-safe? What if those methods that modify those variables are synchronized? Would the class then be considered thread-safe? I guess my current understanding is that synchronized methods make the object (either instance object for instance methods, or class object for static methods) thread-safe.

Thanks in advance!

Re: About Question com.enthuware.ets.scjp.v6.2.496 :

Posted: Tue Sep 20, 2022 9:40 pm
by admin
Right, just because a method modifies instance or class variables of an object does not make that method or object thread- unsafe.

Yes, if a method is synchronized it means that that method is thread safe but that does not mean the object itself is also thread safe because there might be another unsynchronized method that modifies the instance fields. So, you have to look at the class in totality to determine whether it is thread safe or not.

Re: About Question com.enthuware.ets.scjp.v6.2.496 :

Posted: Wed Sep 21, 2022 2:14 pm
by cjgiron
Hi Admin,


The double negatives in this line of your answer are a little confusing:
just because a method modifies instance or class variables of an object does not make that method or object thread- unsafe.
Are you saying that, even if a class contains a method that modifies instance or class variables, the method or object of that class could be still be thread-safe?

Re: About Question com.enthuware.ets.scjp.v6.2.496 :

Posted: Wed Sep 21, 2022 7:00 pm
by admin
That is correct.