
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the use of Object Cloning in Java?
The object cloning is a way to create an exact copy of an object. For this purpose, the clone() method of an object class is used to clone an object. The Cloneable interface must be implemented by a class whose object clone to create. If we do not implement Cloneable interface, clone() method generates CloneNotSupportedException.
The clone() method saves the extra processing task for creating the exact copy of an object. If we perform it by using the new keyword, it will take a lot of processing to be performed, so we can use object cloning.
Syntax
protected Object clone() throws CloneNotSupportedException
Example
public class EmployeeTest implements Cloneable { int id; String name = ""; Employee(int id, String name) { this.id = id; this.name = name; } public Employee clone() throws CloneNotSupportedException { return (Employee)super.clone(); } public static void main(String[] args) { Employee emp = new Employee(115, "Raja"); System.out.println(emp.name); try { Employee emp1 = emp.clone(); System.out.println(emp1.name); } catch(CloneNotSupportedException cnse) { cnse.printStackTrace(); } } }
Output
Raja Raja
- Related Questions & Answers
- What is object cloning in Java?
- Advantages of Object cloning in Java
- PHP Object Cloning
- Cloning in Java
- What is the use of proxy() object in JavaScript?
- What is the use of map object in javascript?
- What is the use of constructor in Java?
- What is the object class in Java?
- What is the use of marker interfaces in Java?
- What is the use of setBounds() method in Java?
- What is the use of StrictMath class in Java?
- What is the use of Thread.sleep() method in Java?
- What is the use of parametrized constructor in Java?
- What is the use of default methods in Java?
- What is the use of page object in JSP? Need an example.
Advertisements