File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Copy Constructor concept n Java Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Copy Constructor concept n Java" Watch "Copy Constructor concept n Java" New topic
Author

Copy Constructor concept n Java

Reddy Prasad
Greenhorn

Joined: Mar 09, 2006
Posts: 1
Hi Friends,

I had a doubt is there any concept like copy constructor in Java like C++. If it is there, how can it be performed in Java.Please give the reply with example.


Thanks and Regards,
Prasad


prasad
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, welcome to the ranch!!

Would that look like this?

You can certainly write the code to make the constructor copy the appropriate fields from old to new, but it's not there automatically. I think the only one of these I've used in the wild is the Attributes object in the XML DOM.

oldWidget.clone() might be an alternative. The object must be written to clone properly. The code would look an awful lot like the copy constructor as it copies each field from old to new. Look at the doc for clone() on the Object class.

Either way, if you're writing the copy code it will be your choice whether to make deep copies or not.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56175
    
  13

This forum is for questions on JDBC. I've moved this topic to a more appropriate forum: Java in General (beginner).


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24049
    
  13

Strangely enough, we had a discussion about this just this morning!


[Jess in Action][AskingGoodQuestions]
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
hardly strange, a new course period has AFAIK started at many schools...


42
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Copy Constructor concept n Java
 
Similar Threads
implement Java Programming logics
Constructor
Virtual Constructor
Sylvan's Mock Question
Are there copy constructors in Java like in C++?