how to know if an object in java support clone ? or it has copy C’tor ? in run time
please provide an example ,
thank you
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
To check if supports clone check
The above will tell you if the object implements Cloneable and default clone is supported or not.
But there are cases when classes dont support clone method but they provide you with some API which will create a copy of the instance, for such methods you will have to look at the API documentation or source code.
Update : Why instanceof check doesn’t guarantee a successful clone:
1-> Consider the below code snippet
2-> Consider the 2nd case below
So though
instanceofcheck will pass correcltly eitherclonecannot be called orclonewill throw exception.