Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java :Questions on clone method

I got these asked in an interview.

  1. Do we need to take care of clone method in a concurrent environment ? Can we synchronize the clone method ?

  2. Does it make any sense to use clone method in singleton classes ?

I did not have convincing answers for this during the interview.

like image 871
user2434 Avatar asked Feb 09 '12 02:02

user2434


1 Answers

  1. Probably yes 99.99% of the times but you just have to think at clone like any other methods of your class and synchronize it if necessary depending on your specific context. There is nothing wrong in synchronizing a method that is not synchronized in its Base class. On the other hand, not synchronizing a method while overriding a synchronized one is probably a mistake even if the code compiles fine and no warnings are issued...

  2. A convincing answer would probably be NO with a couple of words on the singleton design pattern.

like image 84
Marsellus Wallace Avatar answered Sep 23 '22 16:09

Marsellus Wallace