Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamically Adding Attributes and Methods in class

 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rancher

I have a class. What I want is to add attributes and there corresponding getter/setter dynamically. Can I do it using reflection. Please give me any hint / tutorial

Thanks
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you not use the Decorator pattern? Must you actually modify the class itself?
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lamont

I am sorry I could not get you. Please ellaborate

Thanks
 
Scott Escue
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amir,

Took a look at this explanation of the Decorator Pattern. It's hard to say without knowing what you're trying to achieve, but this may help.

And to answer your original question, no. I don't believe you can dynamically add members to your classes or objects in Java.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create classes dynamically at runtime. See BCEL.

You can use reflection to create dynamic proxies too. See this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic