aspose file tools*
The moose likes Java in General and the fly likes Assigning a java class to an identical one with different namespace Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Assigning a java class to an identical one with different namespace" Watch "Assigning a java class to an identical one with different namespace" New topic
Author

Assigning a java class to an identical one with different namespace

baran tamer
Ranch Hand

Joined: Jan 22, 2009
Posts: 33
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

Joined: Aug 03, 2008
Posts: 9303
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41906
    
  63
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?


Ping & DNS - my free Android networking tools app
Adam Michalik
Ranch Hand

Joined: Feb 18, 2008
Posts: 128
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

Joined: Jan 22, 2009
Posts: 33
dozer does the trick , thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Assigning a java class to an identical one with different namespace