Take the 2-minute tour ×

This may sound like a silly question, however i am trying to test my game under different circumstances using reflection. I was wondering if their was anyway to dynamically create an object to contain certain methods, i know i can use proxies, but then i am limited to the methods declared in the interfaces i choose to use in the proxy so i have to create a new interface for each thing i want to add to my object that i am creating. I am hoping to access each method using reflection. I know there are libraries that do this so i am sure that this is possible and i am hoping to not have to install libraries, as i will have to deal with a new api.

share|improve this question
This isn't clear; it sounds like you're asking how to dynamically add methods to an object? –  Oli Charlesworth May 26 '13 at 17:56
yeah or create an object at runtime with a given set of methods –  Popgalop May 26 '13 at 17:57
That doesn't make a great deal of sense in Java. How could such an object possibly be used in code? –  Oli Charlesworth May 26 '13 at 17:58
as i stated with reflection –  Popgalop May 26 '13 at 18:00
This sounds a little weird. If you're only ever going to create and interact this object with reflection, then what's the point? (FYI, I think the answer to your question is "no", but perhaps there's some weird byte-code manipulation tricks you could pull.) –  Oli Charlesworth May 26 '13 at 18:02
add comment

1 Answer

In languages like C, you can pass function references as parameters to another function or procedure. Is this what you are referring to? You want to pass a reference to a function to a method about which the method may not have advance knowledge?

You can't pass function references as a parameter in Java. It isn't allowed. But the workaround for this is exemplified by the abstract factory pattern. This pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.

share|improve this answer
add comment

Your Answer


