Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Generalize instantiation of a data class

 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a requirement wherein I have several data classes (classes which just have members and their getter and setters) and I need to instantiate them using a map that is passed to it. The map has the name value pairs. I want to generalize this process instead of doing this specifically in the constructor of each data class.

One option is to define a parent class of all the data classes and do introspection in its constructor and use the hashmap for setting the values.

But is there an already existing standard way of doing it so that I do not end up reinventing the wheel.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Satya Maheshwari,

I'm grossly confused after reading your post (or I'm not aware of anything like this )

Satya Maheshwari wrote:I have a requirement wherein I have several data classes (classes which just have members and their getter and setters)


Is it same as JavaBean?

Satya Maheshwari wrote:I need to instantiate them using a map that is passed to it


Passed to what? to class? What does Map contain? What is its key and value? (pardon me if I sound rude - I don't mean to )

Satya Maheshwari wrote:and use the hashmap for setting the values


So, is it that Map will contain values of specific fields? In that case, I guess reflection APIs might help.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anayonkar, thanks for replying!!

Basically I have several entity beans on top of corresponding DB tables which I mentioned as data classes in the post. Yes reflection is the way I would do it if there isn't any already existing pattern for doing it.
 
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Satya Maheshwari wrote:I have a requirement wherein I have several data classes (classes which just have members and their getter and setters) and I need to instantiate them using a map that is passed to it. The map has the name value pairs. I want to generalize this process instead of doing this specifically in the constructor of each data class.

One option is to define a parent class of all the data classes and do introspection in its constructor and use the hashmap for setting the values.

But is there an already existing standard way of doing it so that I do not end up reinventing the wheel.


I'm not aware of any existing Java API that allows you to populate instance variables from a Map. My vote also goes to reflection. It should be quick and painless to write a generic method to do that. I'd image it looking like this:
 
There's a way to do it better - find it. -Edison. A better tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic