Sie sind auf Seite 1von 2

Advertise Us | Ask Question | forum | login|

Basics of Java
OOPs Concepts
Advantage of OOPs
Naming Convention
Object and Class
Method Overloading
Constructor
static variable,method
and block
this keyword
Inheritance(IS-A)
Aggregation(HAS-A)
Method Overriding
Covariant Return Type
super keyword
Instance Initializer
block
final keyword
Abstract class
Interface
Runtime Polymorphism
static and Dynamic
binding
Downcasting with
instanceof operator
Package
Access Modifiers
Encapsulation
Object class
Object Cloning
Java Array
Call By Value
strictfp keyword
API Document
Command Line
Argument
String Handling
Exception Handling
Nested Classes
Multithreading
Synchronization
I/O
Serialization
Networking
AWT
Event Handling
Swing
LayoutManager
Applet
Reflection API
Collection
JDBC
Java New Features
RMI
Internationalization
<< prev
<< prev
The clone() method (Object Cloning in Java)
The object cloning is a way to create exact copy of an object. For this purpose, clone() method of Object
class is used to clone an object.
The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create.
If we don't implement Cloneable interface, clone() method gives CloneNotSupportedException. The clone()
method is defined in the Object class. Syntax of the clone() method is as follows:
1. protected Object clone() throws CloneNotSupportedException
Why use clone() method ?
The clone() 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 that is why we use object cloning.
Example of clone() method (Object cloning)
1. class Student implements Cloneable{
2. int rollno;
3. String name;
4.
5. Student(int rollno,String name){
6. this.rollno=rollno;
7. this.name=name;
8. }
9.
10. public Object clone()throws CloneNotSupportedException{
11. return super.clone();
12. }
13.
14. public static void main(String args[]){
15. try{
16. Student s1=new Student(101,"amit");
17.
18. Student s2=(Student)s1.clone();
19.
20. System.out.println(s1.rollno+" "+s1.name);
21. System.out.println(s2.rollno+" "+s2.name);
22.
23. }catch(CloneNotSupportedException c){}
24.
25. }
26. }
Output:101 amit
101 amit
download the example of object cloning
As you can see in the above example, both reference variables have the same value. Thus, the clone()
copies the values of an object to another. So we don't need to write explicit code to copy the value of an
object to another. If we create another object by new keyword and assign the values of another object to
this one, it will require a lot of processing on this object. So to save the extra processing task we use
clone() method.

New: JAXB | Junit
Home Core Java Servlet JSP Struts2 Mail API Hibernate Spring Android Quiz Projects Interview Q Comment Forum
723 13
8
4
converted by Web2PDFConvert.com

Tweet Tweet 89 Like 11k 3
Like the www.javatpoint.com on facebook / google+ / twitter / subscribe to get latest updates
Sitemap Core Java Servlet JSP Struts2 Hibernate Spring Android Interview Questions
javatpoint.com is developed to provide easy and point to point learning and training in detail. Examples might be simplified to improve reading and
basic understanding. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. If there is any
mistake, please mail to sonoojaiswal1987@gmail.com or sonoojaiswal@javatpoint.com. We provide assurance of 90% interview questions.
While using this site, you agree to have read and accepted our terms of use and privacy policy.
2011-2013 Javatpoint. All Rights Reserved.
javatpoint.com
Like
11,704 people like javatpoint.com.
Facebook social plugin
723 13
8
Google +
4
converted by Web2PDFConvert.com

Das könnte Ihnen auch gefallen