Just wondering if there is a way to clone a NamingEnumeration in Java? My program searches an LDAP server for people and it can take a few seconds to complete. To ... |
I have written this clone method for when the parent of the Employee class is abstract and the clone() method in the parent class is abstract.I wanted to copy the primitive ... |
Perhaps I don't understand how clone() works. Shouldn't the return value equal the caller?
int[] nums = new int[] {0, 1, 2};
int[] list = nums.clone();
nums.equals(list); //returns false. Why?
for (int ket = 0; ...
|
Is there any reason why enums in Java cannot be cloned?
The manual states that
This guarantees that enums are never cloned, which is necessary to preserve their "singleton" status. ... |
In Java 1.5, how can I clone an instance of java.io.CharArrayWriter?
CharArrayWriter x = new CharArrayWriter(200);
x.write("foo bar bob");
CharArrayWriter y = x.clone(); // Object.clone() is not visible!!
Thanks,
mobiGeek
|
I have never seen clone() method put to use in any real code. I was reading about it and felt that its use could make the code very cumbersome.
is there any ... |
This code:
class RawStringIterator {
java.util.Stack<State> stateStack = new java.util.Stack<State>();
RawStringIterator(RawStringIterator i) {
...
|
|
In Eclipse I just click above the tab and click on "New Editor". In NetBeans is "Clone". In IntelliJ Idea I didn't find this option yet. What I do now is ... |
I'm customizing mxGraph java editor that comes with jGraph. What happens with the value object when a node is "cloned" by dragging an edge out of it? Does the new node ... |
I am writing a custom clone method for each entity. for deep copy is there a way to detect circular references or do I have to manually figure it out and ... |
I would need some help with the following code if you are kind.
Basically i have a tree node that remembers it's parent node, depth-level and his current state(a 2D array).
Most ... |
I'm writing a program in Java that prints PDF files of Bingo Cards. Each page is one card. To make it easy for me, I created a template PDF file with ... |
Context: We are using the SWT Browser control to programatically navigate web sites. When a given page has a list of links, we click one, listen for the BeforeNavigate event, abort ... |
I am creating a deep clone for some object. The object contains a Random.
Is it good practice to retrieve the seed from the Random? If so, how? There isn't a Random.getSeed().
... |
Clone arr3----> store the result in a variable arr6
static long[] arr6 = (long[])arr3.clone();
|
Clone does not call the object constructor to create a copy of the object. So what algorithm does clone use ?
I am looking for implementation details of the native ... |
I saw this term used a lot in Joshua block's book effective java specialy on the item related to the method clone , so can some explain what does ... |
I read one paragraph from the Internet regarding cloning. But I didn't quite get it, so can someone explain it clearly?
If the class has final fields, these can't be ... |
I'm a developer in training, with some spare time on my hands. I am trying to re-continue a discontinued project (BigBrother, http://forums.bukkit.org/threads/sec-bigbrother-1-11-0-catch-griffers-red-handed-1060.12879), and I don't have access to the ... |
I am having a Java bean, which i want to clone it to get a new instance, but in the new instance, I want few of the properties to have values, ... |
i need the encrypted data in byte[] exactly the way it was 'in-appearence' of the string for decryption process after receiving it at the servlet... if i use getBytes() the encrypted file wont look the same anymore, wont it? original text... (String) text = "IAMWHOIAM" text being signed with private key into byte[] format digital cert.. (Byte[]) cText= [B@84aee8b; the text ... |
I can make StringBuffer a verb, can't I? I am implementing a cloneable interface, and it mostly seems to work correctly, except for the following. I have a class Shape, with data elements Color color; StringBuffer description; The clones seem to work correctly, except that when I try to run my setDesc method to change the description on the clone, it ... |
Hi, What i need to know, if i clone an instance of a class Box b = new Box() ; b.height = 100 ; Box a = b.clone() ; a.height = 200 ; here b.height = ?? is it 100 or 200 a and b refer to the same istance, any changes in a will be applied to b, or a ... |
|
i was reading java book and i did not get the clone part. int [] p = new int [100]; int [] s = (int[]) p.clone(); Which of the following statements will change the last value of p to 75? A) p[99]=75; B)p[100]=75; C)s[99]=75; D)s[100]=75; E) two or more of the answers will chane i to 75 [ September 11, 2005: ... |
what happend if we apply the clone methode on singleton object? already i tried , prob is : as per my knowledge we can apply the clone methode on any class object which is implements clonneable interface.but in case of singleton it is giving like clone() "methode having protected access",why this is comming for singleton class only,(actually singleton means some design ... |
Originally posted by omar bili: ok, got it thanks, but the thing is that my main class does not implement the clone method, so i cant use the super.clone(); maybe ill have to create my own clone method from nothing, and copy all the variables thanks for the help Omar In order to support clone(), you ALWAYS have to ... |
public class Test { public static void main(String[] args) { try { TestSub testSub = new TestSub(); testSub.clone(); } catch(CloneNotSupportedException c) { c.printStackTrace(); } } public Object clone() throws CloneNotSupportedException { return super.clone(); } } class TestSub extends Test implements Cloneable { public Object clone() throws CloneNotSupportedException { return super.clone(); } } although the above code is pretty blank, can I ... |
Backing up data is one case, for example if you want to support an undo feature. You might also use clone if you anticipate making large changes to data and do not want these changes to persist. Finally, you can use clone if you are creating a new object where you want it to have some of the properties of some ... |
Originally posted by Pavel Kubal: Now it makes sense For a moment, consider the question - What exactly makes sense about copying array elements here and there? Let's look at an array. An array is a construct that exposes a contract containing three operations. These operations are symbiotic - they cannot be separated. You can define these operations as: ... |
hi, Class A hold a reference to object O. This object o is passed to other classes say Class B, C, D. I need class A to maintain its own copy means the changes made to object O at classes B,C,D should not affect the copy of class A. if i clone object O and send it to classes B, C, ... |
Hi Guys : Im using a standard serializing deep clone method on an object, however, upon cloning, the initialized object values in the clone are not matched the ones in the object being cloned. That is Class CloneMe { Point x = new Point(1,1); public Point getX() {return x;} public setX(Point x1) {x = x1;} } CloneMe parent = new CloneMe(); ... |
I have understood the concept of cloning but i have doubt in implementing it practically. The following code will show a compile time error : class A { private int x; public A(int i) { x = i; } } public class CloneDemo1 { public static void main(String args[]) throws CloneNotSupportedException { A obj1 = new A(37); A obj2 = (A)obj1.clone(); ... |
Hi Sridhar, It depends on the writer of the singleton class. If you look closely then you will see that clone method of Object class is protected , so unless you override that in you singleton class with public visibility , the clone method cannot be made use. Even your singleton class should implement the Cloneable interface to be cloned successfully. ... |
We have a clone() defined in Object class. If we try to execute a class overriding clone(), it throws CloneNotSupportedException. But if we implement Cloneable interface(which is a marker interface), everything goes fine. Now the question is, why do not we have a clone() in Cloneable interface rather then in Object class. Is it in the Object only because we require ... |
|
37. clone coderanch.comIf they didn't explicitly provide a public clone() method, then the chances are that cloning the object is not a good idea; something complicated like a Document from a DOM is not likely to be susceptible to the default shallow-copy done by clone(). So don't be tempted to try jiggery-pokery to gain access to the clone() method. Perhaps the API you ... |
in this case there is no difference. Its like copying the reference of one array to another. Array Objects has their own overriden clone() method, which only does shallow copying. i.e. something like assinging the an array reference to another array reference variable. Inorder to get your own clone behaviour, you need to write a public method clone() in your class ... |
39. clone() coderanch.comBy using File g=f; you only create a new reference to point to the same instance. That means that any manipulation of that reference will result in "the same" change in the first, as it is the same instance. clone() creates a copy of the instance, a new instance. The new one is independant from the first. Nomatter what you do ... |
|
A shallow clone creates another instance whose fields point to the same objects as the original originalObj clonedObj |\ / | | \/ | |/ \ | field1Obj field2Obj A deep clone not only makes a copy of the object, but copies of all of the objects that it's fields are pointing to. originalObj clonedObj |\ / | | ... |
|
A shallow copy just copies all mutable object references as is. A deep copy clones every object so that the references refer to different objects. Shallow copy: Shallow copy example import java.util.Map; import java.util.HashMap; public class Shallow implements Cloneable { private int x = -3; private String name = "Shallow"; private double[] dArray = {1.2, 3.7, 4.9}; private Map map; public ... |
|
45. CLONE coderanch.comThink of a data structure as a tree. A shallow clone clones the root of the tree and shares the subtrees of the original tree's root. A deep clone is a recursive copy, i.e. all data structure components in the clone are copies of those in the original. In this case every non-leaf node in the tree would be a copy ... |
On page 225 of Peter van der Linden's Just Java 2 (5th edition) exercise 5 is written as this: "write some code to clone an object of your class. Change your version of clone() to do a deep copy for your class. Run the clone program again, and make it print out enough details that you can tell the difference between ... |
|
Mahesh's example omitted a crucial point: clone() returns an Object, and you have to cast the returned object to the correct type: Cloneex dup = (Clonex) ex.clone(); Now, there are three other problems with the example: you'll get a CloneNotSupportedException when you run it. To make something cloneable, you have to declare that the class "implements Cloneable". If you don't, clone() ... |
49. clone() coderanch.comI'm not an expert at clone methods, but I think the idea is that you call super.clone(), and then you must do some additional work to clone the instance variables of your subclass. You don't just return what super.clone returns because as you say, that would just copy the super class object. |
import static java.lang.System.out; public class CloneTest implements Cloneable { private int count; private String str; CloneTest() { count = 10; str = new String("hello"); } public static void main(String[] args) throws CloneNotSupportedException { CloneTest ct = new CloneTest(); ct.test(ct.clone()); out.println(ct.count); out.println(ct.str); } private void test(Object obj) { CloneTest ct1 = (CloneTest)obj; ct1.count = 20; ct1.str = new String("again_hello"); } } |
import java.io.*; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class LeagueWindow extends Observable implements ActionListener { TeamLeague CurrentLeague; TeamLeague BackupLeague; LeagueViewer CurrentView; WinWindow WinningScores; WrongWindow WrongScore; JButton AddButton; JButton EditTeam; JButton SaveLeague; JButton RollBack; JButton QuitLeague; JFrame MainFrame; public LeagueWindow(TeamLeague ThisLeague) { //first, place the ThisLeague object as the current league CurrentLeague = new TeamLeague(ThisLeague); //add in ... |
|
Well... I am only guessing, so hopefully someone more skilled in Java like Mr. Friedman-Hill can instruct us. But I would guess this. a) First of all, class Object actually implements a protected clone() method. This method uses reflection to create a new instance and then do a shallow copy. However, the method checks whether the subtype to be cloned is ... |
54. clone( ) coderanch.comIf you check the API for ArrayList.clone(), you'll learn it's a shallow copy. A new ArrayList object is returned, but it contains the same elements (not new copies) of the original ArrayList. To see how this works, try this little program: import java.util.ArrayList; public class ArrayListCloneTest { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add(new String("abc")); list.add(new ... |
55. clone() coderanch.comCalling super.clone is not required. It's just used often because Object's implementation copies all values for you, and it removes a lot of work for you if you have many fields. If you look at the contract of clone(), it doesn't say you need super.clone(). In fact, there are no real requirements. The clone may be of a different class, it ... |
I have a list of Objects of a class. This class has several instance variables, most of them are String and couple of others are BitSet type. At runtime this list of objects is huge. It takes a lot of memory. I need to clone this list at one stage. But cloning this huge list is affecting application performance badly. Here's ... |
Hi .. We have two types of cloning in java... Shallow and deep. I know that if you use normal obj.clone() means we will get shallow one and to get deep , we need to go serialization. I have doubt here ... When we want to go deep or shallow copy ... Any links over that or explanation please .. Thanks ... |
All. I have a delema , I want to create an immutable message. It is not strictly immutable, I just want to create a copy of a message before sending it. The code looks similar to this: public abstract class Message implements Cloneable{ private final T body; protected Message(T body) { if(body == null) throw new NullPointerException("Message body is ... |
|
I have a question on custom cloning. I am giving a simplified example of object structure in my project. Suppose I have following classes: public class Product { private int id; private String name; private boolean isAvailable; private String manufacturer; private Date expiryDate; private List standardIngredients; private List customIngredients; private String location; } class Ingredient { private String itemName; private float ... |
HI Ranchers, I am facing a difficulty while trying to create a clone(just to test) of singleton(Singleton1) class. I am getting an error of "clone() has protected access in java.lang.Object". And I don't understand this error. I know by default Object is a super class of every one that we create(directly or indirectly). And hence when I test instanceof , it ... |
62. Cloning coderanch.comclass CoffeeCup implements Cloneable { public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException e) { // This should never happen throw new InternalError(e.toString()); } } } in another class i used: CoffeeCup original = new CoffeeCup(); original.add(75); // Original now contains 75 ml of coffee CoffeeCup copy = (CoffeeCup) original.clone(); Now i got exact copy of CoffeeCup. my ... |
as i know obj1=obj.clone() in this a new obj1 will be made in the memory same as the obj and by doing obj1=obj a new reference of obj is made named as obj1 in the memory new obj will not be made it will point the old obj .. please consider my answer after confirming from at least one person more ... |
I have a class Book, the code can be null. When I am using clone like this, I am getting NullPointerException, when code is null. But I want to copy even when code is null. public class Book implements Cloneable{ private String isbn; private String book_name; private String code; public String getIsbn(){ return isbn; } public void setIsbn(String isbn){ this.isbn=isbn; } ... |
//File: TestClass.java public class TestClass implements Cloneable { private int[] array = {0, 0, 0}; public TestClass(int x, int y, int z) { this.set(x, y, z); } public void set(int x, int y, int z) { this.array[0] = x; this.array[1] = y; this.array[2] = z; } public int getX() { return array[0]; } public int getY() { return array[1]; } public ... |
66. clone().? java-forums.orgClone only does something useful if the class you are calling it on implements it in a useful way. For example, one might want to clone some form of array - which would mean you would want to get an array just like the original, but a copy. This becomes difficult when objects contain other object types - how do you ... |
|
|
69. cloning forums.oracle.com |
|
|
72. cloning forums.oracle.comIs there any other alternative to cloning. In one of my application I need to clone in just one case. So it doesent seem to be a right idea to implement Cloneable for that object just to use it in one place; Please let me know if there are any other alternatives. |
73. clone()? forums.oracle.com |
74. Cloning forums.oracle.com |
75. Java clone forums.oracle.com |
|
77. clone forums.oracle.com |
And switching to vector may still not have solved it... It has synchronization built-in but not for iterating: The Iterators returned by Vector's iterator and listIterator methods are fail-fast: if the Vector is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a ConcurrentModificationException. ... |
I realize that Object.clone() is a useful method when used in your own classes to override the inherited version, if written in such a way that a deep copying will be accomplished. However, I wonder if else there is ever any point with just calling clone(). I mean, why not just assign the object you want to copy to a new ... |
80. Clone forums.oracle.comThanks for ur reply when i tried your code its throwing exception cloning not supported Actually i have a gui where it user selects a particulat test and all the testtopics will be dispayed now he can make changes to setings in testopics such as change number of questions in topic and so on. Now there will be "cnacel" button which ... |
|
The clone()/Cloneable practice is discouraged in some circles, including (IIRC) members of the Java team who called the implementation a mistake. Like most of these recommendations, there are times and places for it if it's what you're looking for. I've run into clone() issues myself, and even though they could have been prevented if I didn't make stupid mistakes, everybody makes ... |
|
|
85. clone( ) forums.oracle.com |
Now i think there is no way to identify that that how a object is created. There are several ways how a object can be created at runtime. Like using cloneing also u can create a object after deserializing the sate of an object, which may be serialized before object can also be obtain using a class loaded from a stream ... |
|
88. .clone() forums.oracle.comI work with a one dimensial array a [ ] that contains unsorted integers and I like to get the minimum and the position of that minimum in my array. To solve that problem I thought to clone my array a [ ] to b [ ], so I get 2 arrays. Then I sort array b, take b [ 0 ... |
|
|
|
|
|