Solved

Copy constructors in Java

Posted on 2000-02-17
11
394 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
json example 39 131
Core Java. What output will be and why ? 6 65
nextBoolean(double p) for Random class 3 35
tomcat not starting 6 26
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

919 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

17 Experts available now in Live!

Get 1:1 Help Now