• Post Reply Bookmark Topic Watch Topic
  • New Topic

Does Java Support Copy Constructors?  RSS feed

 
Ranch Hand
Posts: 207
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I wanted to ask 1 question, and question is that, whether java support copy constructor or not.

I read somewhere regarding this->


They are saying that the line->

Complex c2 = new Complex(c1);  is a copy constructor.

But, what I think is that this line seems to be a parameterized constructor and not copy constructor.
Also. java does not support pass by reference, then how copy constructor concept possible in java?

Thanks in Advance.
 
Marshal
Posts: 56871
174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you think a copy constructor would have no parameters? Surely it has the parameter of the object you want to take a copy of.
What has pass by reference to do with this question? Surely pass by reference is irrelevant to this particular question.

Please always tell us where you saw such code, to avoid copyright problems and so we can see the original for ourselves.
 
Saloon Keeper
Posts: 8110
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A copy constructor is simply a constructor that takes an object of the same type, and constructs a copy of it. The constructor you showed does exactly that. Why does it need to pass parameters by reference? Why shouldn't it have parameters?
 
O Shea
Ranch Hand
Posts: 207
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
What has pass by reference to do with this question? Surely pass by reference is irrelevant to this particular question.



Since in C/C++ always reference is passed in the argument(since we use & sign), but since in java there is no pass by reference, that's why I am asking about it.

Campbell Ritchie wrote:
Please always tell us where you saw such code, to avoid copyright problems and so we can see the original for ourselves.


Sorry for this, next time I will take care of it.
Reference:Link

Stephan van Hulst wrote:
A copy constructor is simply a constructor that takes an object of the same type, and constructs a copy of it. The constructor you showed does exactly that. Why does it need to pass parameters by reference? Why shouldn't it have parameters?



Yes, definitely, it has parameters, but in parameter usually we pass address of object, but in java how we can pass address/reference?
Shall we pass, copy of reference?
 
Sheriff
Posts: 22958
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

O Shea wrote:Yes, definitely, it has parameters, but in parameter usually we pass address of object, but in java how we can pass address/reference?
Shall we pass, copy of reference?



In Java the parameter is always a copy of the reference to the object. You don't have any option to do otherwise.
 
Campbell Ritchie
Marshal
Posts: 56871
174
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

O Shea wrote:. . . Since in C/C++ always reference is passed in the argument(since we use & sign), but since in java there is no pass by reference . . .

I am not sure that is correct; C++ supports pass by reference if you use the address operator &. I thought (but may be mistaken) that C doesn't support pass by reference, but a pointer can be passed by value instead. Passing a pointer allows C to mimic pass by reference.

Yes, definitely, it has parameters, but in parameter usually we pass address of object, but in java how we can pass address/reference? . . .

You pass the variable, which hides the reference, and that reference is copied and passed by value.

Don't allow the similar appearance of the code to C++ mislead you. Java® is a completely different language, and it is dangerous to assume that something which is true about C++ is also true about Java®.
 
O Shea
Ranch Hand
Posts: 207
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
Don't allow the similar appearance of the code to C++ mislead you. Java® is a completely different language, and it is dangerous to assume that something which is true about C++ is also true about Java®.


Yes sure.
But, then what is conclusion, whether java support Copy constructor or not?
Because here is written that Java won't support Copy Constructor-> Link
 
Java Cowboy
Sheriff
Posts: 16078
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In C++, the term "copy constructor" has a specific meaning.

As you have seen from the previous anwers, you can do the same thing in Java, but people don't commonly treat this as a special kind of constructor, or even call it a "copy constructor".

If you're comparing C++ and Java, then one of the main differences to be aware of is how variables are handled. In C++, a variable of a non-primitive type is the object itself. If you pass it to a function, the whole object is copied. That is not the case in Java; in Java, a variable of a non-primitive type is a reference to an object. In Java, there is no way to have a variable that is the object itself, as in C++.

Because of the way that variables work in C++, it's important to be aware of how objects are copied. In Java, objects are not automatically copied when you pass them to a method and you rarely have to copy a complete object explicitly, so in Java this is not really an issue that people have to think about.
 
O Shea
Ranch Hand
Posts: 207
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jesper de Jong wrote:In C++, the term "copy constructor" has a specific meaning.

As you have seen from the previous anwers, you can do the same thing in Java, but people don't commonly treat this as a special kind of constructor, or even call it a "copy constructor".

If you're comparing C++ and Java, then one of the main differences to be aware of is how variables are handled. In C++, a variable of a non-primitive type is the object itself. If you pass it to a function, the whole object is copied. That is not the case in Java; in Java, a variable of a non-primitive type is a reference to an object. In Java, there is no way to have a variable that is the object itself, as in C++.

Because of the way that variables work in C++, it's important to be aware of how objects are copied. In Java, objects are not automatically copied when you pass them to a method and you rarely have to copy a complete object explicitly, so in Java this is not really an issue that people have to think about.



Ok. So we can say that Java dosen't support copy Constructor, it's just the value is copied, not the whole object.
 
O Shea
Ranch Hand
Posts: 207
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, still I have doubt that in java also, Copy Constructor is doing it's task perfectly i.e, copy the value of 1 object to another.
However in C++ it is done by passing the reference variable itself(i.e, pass by reference), but in java it is done by passing the copy of reference(i.e, pass by value).
So, how can we say that Java, won't support Copy Constructor???
 
Stephan van Hulst
Saloon Keeper
Posts: 8110
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To have any meaningful discussion about this topic, you have to define what the term "copy constructor" means. My definition is a constructor that makes a copy of an object. According to that definition, you can create copy constructors in java. Does any other definition really matter? The outcome and use cases are the same.
 
O Shea
Ranch Hand
Posts: 207
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:To have any meaningful discussion about this topic, you have to define what the term "copy constructor" means. My definition is a constructor that makes a copy of an object. According to that definition, you can create copy constructors in java. Does any other definition really matter? The outcome and use cases are the same.



I am confused regarding these 2 references->

This site is saying that there is a copy constructor in java-> Site 1

This site is saying that there is no copy constructor in java-> Site 2

That's why I am confused according to these 2 sites.
 
Stephan van Hulst
Saloon Keeper
Posts: 8110
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second link is full of mistakes (for instance, you can have parameterless constructors that are not the default constructor, and constructors are not special types of methods) and also doesn't define the term "copy constructor".

It doesn't matter. In Java, you can write constructors that copy an object. Any comparison with how a language like C++ does it is meaningless.
 
O Shea
Ranch Hand
Posts: 207
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:The second link is full of mistakes (for instance, you can have parameterless constructors that are not the default constructor, and constructors are not special types of methods) and also doesn't define the term "copy constructor".

It doesn't matter. In Java, you can write constructors that copy an object. Any comparison with how a language like C++ does it is meaningless.


Thank you very much for clearing my doubt.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!