if I have a code which has the clone() method but as I know that the duplicate code is bad here(in that application e.g.bank Application),what should I do???
the clone() method is ... |
There are classes:
InterfaceInterval<C extends Comparable<C>, I extends InterfaceInterval<C, I>> extends Comparable<InterfaceInterval<C, ?>>
AbstractInterval<C extends Comparable<C>, I extends AbstractInterval<C, I>> implements InterfaceInterval<C, I>, Serializable, Cloneable
AbstractTimeInterval<I extends AbstractTimeInterval<I>> extends AbstractInterval<Date, I>
SortedIntervalsList<C extends Comparable<C>, I ...
|
why we use Clone() method in JAVA ? (please give the answer in respect of memory constraint). will that reduce memory usage, if yes then how??? Will that reduce the effect ... |
as I understood, the method clone() gives us the ability to copy object (no refernce) in Java. But I also read, that the copy is shallow. So what the point? Which ... |
I'm almost done with this project which involves converting between Celsius, Fahrenheit, and Kelvin and the last thing I need is to figure out the cloning method. The task is "clone, ... |
Last week in our java course we were introduced the Object class and some of its methods
one of these methods was clone() and when our teacher explained us this method ... |
I am reading Effective Java and the book has the below comment on the clone method.
In practice,
a class that implements Cloneable is expected to provide a properly
... |
|
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: ... |
I hava a data object which I want to be able to clone. The clone method will instantiate a new data object without copying the data of the original. Like this; package a; public interface Data { //methods } package a; public abstract class AbstractData implements Data { public Object clone() { //reflection thing return newData; } } public class myData ... |
You can't implement a copy constructor using clone(). However, clone() and copy constructors can sometimes be used for similar purposes. A copy constructor is a constructor that takes an object of the same class and copies the data from that object into the object being constructed. The clone() method is an instance method of every object. When you execute it on ... |
Hi, Bruce Eckel mentions two reasons why the clone() is a protected method: I am just copying the paragraph: "to prevent cloneability in every class you create, the clone method is protected in the base class Object. Not only does this mean that it's not available by default to a client programmer who is simply using the class ( not subclassing ... |
|
Hi, I'm not aware of the requirement to implement the Serializable interface, however I'm aware of requirement to implement the Cloneable interface. The Cloneable interface acts like a flag signaling (the compiler I ass u me) that your objects are prepared to be cloned. Maybe you can check if you're referring to Cloneable. I think that StarWars III goes into more ... |
The rules governing this are subtle, and have been discussed in this forum many times before; use our Search function to search for "clone" in this forum and you'll find full explanations. But in any case, to make clone() work for a class, that class has to do two things: 1) Override clone to call super.clone(); and 2) Implement the (empty) ... |
|
|
|
In Java 1.4 and earlier, all clone() could return was Object. Therefore, you always needed to cast the result to whatever it really is returning. Java 5.0 introduced covariant return types. It basically means that if you override (or implement) a method that is defined to return X, you can now declare to return anything that IS-A X. clone() is one ... |
If you really need to make your class cloneable, then there are some protective measures you can take to prevent attackers from redefining your clone method. If you're defining your own clone method, just make it final like this : public final void clone() throws java.lang.CloneNotSupportedException { super.clone(); } This may be a stupid question .But i could not think beyond ... |
The clone method is protected in the Object class. This is because unless your class implements Cloneable interface, calling the clone method will result in a CloneNotSupported. Since it is protected so only your class can call that method. Any other class cannot call the clone method on the instances of your class. If you implement Cloneable in your class, then ... |
Creating a clone of an object and creating a new object are two different things. Lets say you have a class Car that has member variables like colour, model, and the distance it has travelled. On creating a new object the values these member variables take are the ones you initialise them with in the constructor. But if you create a ... |
|
The clone method is protected in Object class and each class created in java extends Object class. Now if we create a class and declare a method as protected then it's subclasses should be able to instantiate a base class object and call it. Example:- class A { protected void method() { } } class B extends A { public static ... |
I develoloped this rather simple class that implement one item of material. Java Code: /** * */ package practice; /** * @author Ivan Dejanovic * */ public class Material { private int count; private String fisName; private String fisCode; private String name; private String nameAux; public Material() { count = 0; fisName = new String(""); fisCode = new String("");; name = ... |
In the object class, the clone method is defined as protected, which means only subclasses can call it. Now in your creature class, you define it as public, so you can call clone on a creature. However this line (Animation)left.clone(), is trying to call clone on an object of type Animation. I assume that the Animation class does NOT define a ... |
It's the wrong way to do a clone. One problem is if you subclass EnumCard, and don't re-override clone, your clone method will give the wrong class. The bigger problem though is that you just randomly tried code until you got some that worked without understanding what was wrong with your previous code. |
|
In your subclass, you only clone class variables defined in the subclass. You call super clone in the subclass to let the base class clone its own class variables. This is an appropriate division of responsibilities. It appears you are cloning all class variabes in the sub class weather they are declared in the subclass or base class. You can do ... |
(how come you said "javac -version" instead of "java -version"? Anyway, if I compile the original code, as found in the doc, I get: Test.java:4: Incompatible types found: : java.lang.Object required: int[] int ia2[] = ia1.clone(); ? When I compile the code with the cast added, it compiles clean and runs as expected. When I compile your code, I get: Test.java:4: ... |
|
|
"SocketException: Too many open files". That means there are too many sockets being created, which corresponds to AAA being large. Sockets and file handles are a finite resource and you are exhausing them by setting an excessive value for AAA. You need to use a java.util.concurrent.ThreadPool to limit the number of concurrent threads and therefore the total number of concurrent sockets ... |
|
Yes, the idea is that if a class wants to permit cloning then it should implement the Cloneable interface and override the default protected clone method with a public clone method. But unfortunately the Cloneable interface does not define a clone method and the whole thing is a mess really, which is why it is preferable to provide a copy constructor ... |
|
|
Hello people. I have the following clone method for a class: My question is why this clone methot work fine if "v.elements" vector is obtained with the references of the elements vector. I made a clone of MyClass and after some tests, works fine... //... int length; Object [] elements; public synchronized Object clone() { MyClass v = new MyClass(); v.elements ... |