I did a quick google on implementing clone() in Java and found:
http://www.javapractices.com/topic/TopicAction.do?Id=71
It has the following comment:
copy constructors and static factory methods provide an alternative to clone, and ... |
|
clone method vs copy constructor in java. which one is correct solution. where to use each case?
|
I already know, what a shallow copy is, but I'm not able to impliment it. Here's a short example.
public class Shallow {
String name;
int number;
public Shallow (Shallow s) {
this.name ...
|
So lets say I want to make a deep copy of an object, but using its contsructor. So I have:
public class PositionList {
private Position[] data = new ...
|
I am currently working on a Java assignment, and for some reason, it works - but to my mind, it shouldn't ! What I have is a main method, creating three ... |
I have a question about java copy constructors.
I am writing a program where I have a private final instance variable and i'm writing an add method that calls 2 values for ... |
|
How do I build a copy constructor that receive another point (x,y) and copy its values ?
I decide a signature: public Point1 (Point1 other) , but I don't know what to ... |
I hope you will help me understand this Copy Constructor I took more then 2 hours reading reading on websites and I didn't understand anything about it.
I know that a copy ... |
In Josh Bloch's excellent book Effective Java under Item 39 he says:
"[D]efensive copies are made before checking the validity of the parameters, and the validity check is ... |
This is a simple question but if I do
List<Object> list = getObjectsFromDatabase();
This would not be the correct way to handle this?
But this would?
List<Object> firstList = getObjectsFromDatabase();
List<Object> list = new ArrayList<Object>(firstList);
Or if ... |
I have a bit of a problem. I'm making a Finite Automata checker.
Given an input, and the DFA, does it end on a accepting state.
My problem is creating a new ... |
I'm trying to complete a project, and though I have tried, I can't seem to do this. Here's the enum:
public enum Symbols {
/**
...
|
The code below shows a rectangle class using double points, which are also stored in an object. The rectangle and the points in the rectangle are immutable because they do not ... |
During the reading of one of the technical articles of Sun ( Article on Cloning and Serialization ) I found this: public class CloneDemo8 { private int a; private int b; private final long c; public CloneDemo8(int a, int b) { this.a = a; this.b = b; this.c = System.currentTimeMillis(); } public CloneDemo8(CloneDemo8 obj) { this.a = obj.a; this.b = obj.b; ... |
Is there any advantage to implementing the Cloneable interface as opposed to defining a "copy constructor" public class Foo implments Cloneable { private String aString; private int aNumber; private Bar bar; public Foo(String aString, int aNumber, Bar bar) { this.aString = aString; this.aNumber = aNumber; this.bar = new Bar(bar); } public Foo(Foo foo) { this.aString = foo.aString; this.aNumber = foo.aNumber; this.bar ... |
Hi, welcome to the ranch!! Would that look like this? Widget newWidget = new Widget( oldWidget ); You can certainly write the code to make the constructor copy the appropriate fields from old to new, but it's not there automatically. I think the only one of these I've used in the wild is the Attributes object in the XML DOM. oldWidget.clone() ... |
To create an object identical to the original one. To prevent the original being altered.public class Kettle { private int temperature; private int content; private final int capacity; public Kettle(int capacity) { content = 0; //When you buy a kettle it contains no water temperature = Temperatures.ROOM_TEMPERATURE; this.capacity = capacity; } /** * Copy constructor which creates a Kettle object identical ... |
im writing code for a project of mine. and i dont know what copy constructors really are.if i have a class , Dog, in that class i have lets say a constructor, public Dog(int size, int colour, string race){...} and i have a copy constructor, public Dog(Dog arg0){} what exactly does it do? |
|
|
//return the value return studentCompare; } public StudentProg2(StudentProg2 copyObject) { this.studentID = copyObject.studentID; this.examGrade1 = copyObject.examGrade1; this.examGrade2 = copyObject.examGrade2; this.examGrade3 = copyObject.examGrade3; this.assignGrade1 = copyObject.assignGrade1; this.assignGrade2 = copyObject.assignGrade2; this.assignGrade3 = copyObject.assignGrade3; this.totalGrade = copyObject.totalGrade; } } public class StudentTestProg2 { public static void main(String[] args) { StudentProg2 wcu = new StudentProg2("Kristopher Russo " , "CSC142" , "A"); StudentProg2 wcu2 = new ... |
it's seems fine my examples; I'm worried about the fact that something can be a real copy but still a reference; Did I have to write even this ? this( new String(copy._name)); What about clone() method? can't I use it? My problem is that I must get a new copy of a class to modify it and keep the original one ... |
|
The research and the discussions should have told you that it's not clear-cut and you won't get a clear answer this time either. When your class if final it basically boils down to personal style choice and consistency. I'd say that you should be consistent in your choice within a project (don't mix both, or you'll never know where you should ... |
|
technically, Java does not recognise the term "copy constructor". It's purely a C++ concept of constructors written according to specific rules which in Java can't be implemented. Of course Java can have an equivalent of them, passing a template object into a constructor to use for initialisation purposes, but it's not the same. |
|
Regardless I'm obligated once I create every method necessary to test them in a dump demo class to show the teacher each of the methods function properly. This is because at the end of the entire assignment we won't use all the methods, point showing that in object oriented programming it's important to make more methods although we might not need ... |