Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Copy constructors in Java

Posted on 2000-02-17
11
Medium Priority
?
401 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 200 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

618 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