Win a copy of Svelte and Sapper in Action this week in the 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Dynamically Adding Attributes and Methods in class

 
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
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE 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
 
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE 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.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic