
- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java.lang.Object.clone() Method
Description
The java.lang.Object.clone() creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression −
x.clone() != x
will be true, and that the expression −
x.clone().getClass() == x.getClass()
will be true, but these are not absolute requirements. While it is typically the case that −
x.clone().equals(x)
will be true, this is not an absolute requirement.
Declaration
Following is the declaration for java.lang.Object.clone() method
protected Object clone()
Parameters
NA
Return Value
This method returns a clone of this instance.
Exception
CloneNotSupportedException − if the object's class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.
Example
The following example shows the usage of lang.Object.clone() method.
package com.tutorialspoint; import java.util.GregorianCalendar; public class ObjectDemo { public static void main(String[] args) { // create a gregorian calendar, which is an object GregorianCalendar cal = new GregorianCalendar(); // clone object cal into object y GregorianCalendar y = (GregorianCalendar) cal.clone(); // print both cal and y System.out.println("" + cal.getTime()); System.out.println("" + y.getTime()); } }
Let us compile and run the above program, this will produce the following result −
Mon Sep 17 04:51:41 EEST 2012 Mon Sep 17 04:51:41 EEST 2012