About Question com.enthuware.ets.scjp.v6.2.496 :
Moderator: admin
About Question com.enthuware.ets.scjp.v6.2.496 :
I didn't understand the concept of object wrapping.... infact the whole question and answer... can somebody explain me this?
-
- Posts: 2
- Joined: Thu Oct 24, 2013 7:57 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.496 :
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:)
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:)
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.496 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Thu Oct 24, 2013 7:57 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.496 :
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?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.496 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Fri Sep 09, 2022 5:03 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.496 :
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!
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!
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.496 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Fri Sep 09, 2022 5:03 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.496 :
Hi Admin,
The double negatives in this line of your answer are a little confusing:
The double negatives in this line of your answer are a little confusing:
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?just because a method modifies instance or class variables of an object does not make that method or object thread- unsafe.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.496 :
That is correct.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 52 guests