Solved

Copy constructors in Java

Posted on 2000-02-17
11
393 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countX 22 70
How Complex Is This Java Course ? 9 63
Exception after setting jdbc session management 2 38
recursion example 16 69
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 …
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now