Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assigning a java class to an identical one with different namespace

 
baran tamer
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,
I would like to something unordinary i guess.
I have 2 classes which are identical to each other





i would like to assign fields of class book to fields of class magazine.
I can use something like if i know the field names


But in my case i dont know the number of fields and field names of the classes , how can i do an assignment like this generically ?

Thanks



 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you want to be able to copy values of arbitrary number of properties from one class to another. That is possible through reflection. Look at this tutorial and try to create a program that suits your requirements...
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you keep two different classes to begin with? Have you considered using just one class that has a boolean (or whatever is appropriate) differentiator between books and magazines?
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may help: Dozer. It's a framework for JavaBeans mapping - can map any class to any class basing on properties' names. Very easy to use.
 
baran tamer
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dozer does the trick , thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic