• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Java constructor with declaration in another class

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read a lot of articles about Java constructors with declaration in the same class. Now I got a little confused how to make copy constructors when attributes are declared in another class?

 
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you trying to copy? A book or a BookCopy?
 
Alexander Visser
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to make a copy of a BookCopy.
I tried "return new BookCopy()" too, but doesn't work.
 
Piet Souris
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show the code that you used to test the BookCopy class?
 
Alexander Visser
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Piet Souris
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that looks okay. Did you get any errors? And do you know why 'return new BookCopy()' doesn't work?
 
Alexander Visser
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't get any errors. But b2 and b3 both refer to the same object, but they must be different objects. For example: I have a book, named "Java Development" in the library and the library has 10 BookCopies of "Java Development". So I need to change public BookCopy(BookCopy bookcopy), so it will make a new object without referring to the same object.
 
Marshal
Posts: 71629
312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't quote understand what you are trying to do, but I don't think that is how you make a copy constructor. The whole idea is to make two different objects with the same state, but whose state may now change independently of each other.
Don't assign fields to null at declaration time, and make the class final.Declare that field final, and you now have an immutable class with all its advantages, including the fact that you never need to take copies of it.Now, let's imagine your book class is no longer immutable. Let's add a field which will have to change from time to time.Now it is worth creating a copy constructor so you can have an object preserving the old price:-[
 
Alexander Visser
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for additional suggestions.
Hope I can make it more clear.
With class Book I can create a new book, for example "Java Development".
Within class BookCopy there must be two constructors.
First constructor is to create a new BookCopy with a book as parameter.
Second constructor is to create a copy of a BookCopy with a bookcopy as parameter.
If it is not clear, please let me know how I can make it more clear.
 
Saloon Keeper
Posts: 7601
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both your Book and BookCopy classes need copy constructors. The BookCopy copy constructor needs to invoke the Book copy constructor.
 
Alexander Visser
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See code below I've tried, but b2 and b3 still refer to the same object. What did I do wrong?



 
Carey Brown
Saloon Keeper
Posts: 7601
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 7601
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out of curiosity why do you need a BookCopy class? Doesn't the copy constructor in Book give you a Book copy? Now you've got two means by which a Book can be copied: directly via Book copy constructor, and indirectly via BookCopy copy constructor. It would seem that Book copy constructor should be all you'd need. Like this:
 
A magnificient life is loaded with tough challenges. En garde tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic