Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
I am learning GoF Java Design Patterns and I want to see some real life examples of them. Can you guys point to some good usage of these Design Patterns.(preferably in Java's core libraries). Thank you
Why wasn't the .clone() method specified in the java.lang.Cloneable interface ?
I'm currently trying to learn Ruby and I'm trying to understand more about what it offers in terms of encapsulation and contracts. In C# a contract can be defined using an interface. A class which implements the interface must fulfil the terms within the contract by providing an implementation for each method and property (and maybe other things) defined. The individual class that implements a...
I'm programming genetic processes in java for my project and I want simulate the mitosis of a human cell. A human cell contains 23 pairs of chromosome. Mitosis is basically a cell division, or reproduction, in which a cell gives rise to two genetically identical daughter cells. You can find a picture about it here (scroll a little bit down the page): Mitosis I think that this mitosis would be...
I have a Runnable class like: Class R1 implements Runnable { private static final Log LOGGER = LogFactory.getLog(R1.class); private final ObjectClass obj; private final SomeService service; public R1(ObjectClass obj, SomeService service) { this.obj = obj; this.service = service; } @override public void run() { String value = this.obj.getSomeValue(); LOGGER.deb...
If I have: class foo implements Cloneable and then do: bar = new foo(); bar.clone(); I get a shallow copy without needing to write any bar.clone() code like I normally would need to do when I implement an interface. My understanding is that an interface's functions must be filled in by the class implementing it, and Object.clone() has no implementation (as per the docs, "The class Objec...
The objective of prototype pattern is to clone an object by reducing the cost of creation. Here is an example: class Complex { int[] nums = {1,2,3,4,5}; public Complex clone() { return new Complex();//this line create a new object, so is it violate the objective of prototype ?// } } class Test2 { Complex c1 = new Complex(); Complex makeCopy() { retu...
OK, so it's easy to name an interface (or class for that matter) if you can easily think of a noun: User, Window, Database, Stream, etc. What about an adjective or adjective concept? e.g. something that has a timestamp (HasTimestamp, Timestamped, Timestampable...?) or something that is tracked or watched (Trackable, IsTracked, Watchable, IsWatched...?)
I need clarification on the differences between deep copy, shallow copy, and clone in Java
Is cloning good practice in this case? How to do it better? public ModelCollection startParsing() { return parseFeed(new ModelSpecialEntry); } public ModelCollection parseFeed(ModelEntry pattern) { ModelCollection modelCollection = new ModelCollection(); while( condition ) { //TODO: Is cloning the best solution? ModelEntry model = (ModelEntry) pattern.clone(); ...
I have a question about using an object as the variable in a constructor. It might be simple but I just really can't think of what to do and my java book isn't really helping. Say i wanted to do this Fraction f3 = new Fraction(1, 2); Fraction f5 = new Fraction(f3); my constructor for the first object is: public Fraction(int n, int d) { if (d == 0) { numerator = 0; ...
Can we expose interfaces in Ruby like we do in java and enforce the Ruby modules or classes to implement the methods defined by interface. One way is to use inheritance and method_missing to achieve the same but is there any other more appropriate approach available ?
I am trying to clone a DTO. I have taken a DTO Object as shown: public class Employee implements Cloneable { String name; String dept; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dep...
i googled abt this question, but i did not get clear answer.Might be this question may help to some of folks here also. Question : package GoodQuestions; public class MyClass { MyClass() throws CloneNotSupportedException { try { throw new CloneNotSupportedException(); }catch(Exception e) { e.printStackTrace(); } } public static...
In C++ a getter & setter for a private data member is very useful due to the ability to control mutability via a const return value. In Java, if I understand correctly (please correct me if I am mistaken), specifying final on a getter doesn't work that way. Once the caller received the data member reference through the getter, it can modify it, despite it being private... If that's the ca...
Possible Duplicate: What is the use of marker interfaces in Java? What is the use of extending marker interface.
I've read the javadoc for both Object and Cloneable and am just not "getting" something. Can someone please explain to me the performance and/or functional differences to the two following examples: public class Widget { @Override public Widget clone() { // ... return a clone of this Widget } } ..and: public class Widget implements Cloneable { @Override p...
I want to perform a deep copy on an object, does the clone function work to that extent, or do I have to create a function to physically copy it, and return a pointer to it? That is, I want Board tempBoard = board.copy(); This would copy the board object into the tempBoard, where the board object holds: public interface Board { Board copy(); } public class BoardConcrete implements Boar...
I'm having a small problem in Java. I have an interface called Modifiable. Objects implementing this interface are Modifiable. I also have a ModifyCommand class (with the Command pattern) that receive two Modifiable objects (to swap them in a list further on - that's not my question, I designed that solution already). The ModifyCommand class starts by making clones of the Modifiable objects. ...
its easier to explain in code so here Object anObj; anObj = new MyObj(); anObj = new Rectangle(); anObj.clone();//this doesnt exist because its on the root Object class what can i use instead of the Object.clone() method in this example? ----------------------- extra info ------------------------------ I have added extra info but it seems to have gone in the middle of all the answers, so h...
Here is something that I cannot understand. In java.lang.Object the clone() is defined with protected modifier. By definition than it can be accessed by name inside its own class definition, by name inside any class derived from it, and by name in the definition of any class in the same package. Here the Sample class is in another package, and obviously it can't access clone() from the Object...
 /*
  * Copyright 1995-2004 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Sun designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
package java.lang;

A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class.

Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.

By convention, classes that implement this interface should override Object.clone (which is protected) with a public method. See Object.clone() for details on overriding this method.

Note that this interface does not contain the clone method. Therefore, it is not possible to clone an object merely by virtue of the fact that it implements this interface. Even if the clone method is invoked reflectively, there is no guarantee that it will succeed.

Author(s):
unascribed
Since:
JDK1.0
See also:
CloneNotSupportedException
Object.clone()
public interface Cloneable {
New to GrepCode? Check out our FAQ X