Solved

Copy constructors in Java

Posted on 2000-02-17
11
399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

688 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