Solved

Copy constructors in Java

Posted on 2000-02-17
11
395 Views
Last Modified: 2012-06-27
Are copy constructors not used in Java?  I see little or no mention of them in Java books (compared to C++)
0
Comment
Question by:omom
  • 3
  • 3
  • 3
  • +2
11 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 2532239
you can use / implement copy constructors in Java

'public String(String value)
Allocates a new string that contains the same sequence of characters as the string argument.'

but it's much more Java'ish to use .clone() method.

0
 
LVL 8

Accepted Solution

by:
diakov earned 50 total points
ID: 2532276
Look at these articles, that extensivelly cover the issue about overriding Object methods (it does relate) and canonical objects:

http://www.javaworld.com/javaworld/jw-10-1998/jw-10-techniques_p.html

http://www.javaworld.com/javaworld/jw-01-1999/jw-01-object-2.html

Look at the bottom of the page for the second one.

Cheers,
  Nik

0
 
LVL 1

Expert Comment

by:pyxide
ID: 2532285
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 7

Expert Comment

by:Ravindra76
ID: 2535534

I think these copy constructors are not needed in java
0
 
LVL 8

Expert Comment

by:diakov
ID: 2536599
Well, think again. In a complex application, quite often you need to copy an object, for example if you want to pass the reference to another object that might destroy the contents of the referenced object. And if you do not know the type to cast, you might not even be able to copy it manually if that is what you think you can do.

There also other aspects that make an object cannonical, read the article I suggested.

Cheers,
  Nik
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2537961
>> Well, think again. In a complex application...

what about using .clone() instead of copy constructor ? :)
0
 

Author Comment

by:omom
ID: 2539073
I will have to agree with heyhey in that java.lang.Object.clone()

From the docs:

Creates a new object of the same class as this object. It then initializes each of the new object's fields by assigning it the same value as the corresponding field in this object. No constructor is called.

The clone method of class Object will only clone an object whose class indicates that it is willing for its instances to be cloned. A class indicates that its instances can be cloned by declaring that it implements the Cloneable interface.

Heyhey please lock
0
 

Author Comment

by:omom
ID: 2539075
Oops, I tried to reject the answer, but something went wrong.
sorry heyhey
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2539451
no problem.
enjoy Java :)
0
 
LVL 8

Expert Comment

by:diakov
ID: 2539490
Well, things go wrong sometimes. I'll make a question for heyhey.

Nik.
0
 

Author Comment

by:omom
ID: 2540678
thanks,diakov
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 33
javap bin 2 34
windows explorer path to command prompt 5 43
Desingning Refactoring existing code 2 24
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

774 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question