This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Dynamically Adding Attributes and Methods in class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Dynamically Adding Attributes and Methods in class" Watch "Dynamically Adding Attributes and Methods in class" New topic
Author

Dynamically Adding Attributes and Methods in class

Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324
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


"Know where to find the solution and how to use it - that's the secret of success."
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Can you not use the Decorator pattern? Must you actually modify the class itself?
Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324
Lamont

I am sorry I could not get you. Please ellaborate

Thanks
Scott Escue
Ranch Hand

Joined: Jan 20, 2005
Posts: 34
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

Joined: Apr 14, 2004
Posts: 10336

You can create classes dynamically at runtime. See BCEL.

You can use reflection to create dynamic proxies too. See this.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamically Adding Attributes and Methods in class