3

i was wondering if their is any way to dynamically create a static field for a class during run-time using reflection or a related API. If needed i can use the java native interface to accomplish this task if someone could tell me the appropriate steps. I don't want to use any data structures such as a hash-map, as i am doing this completely for fun. Please don't suggest using maps as i am not using this for any real program this is a completely theoretical situation. Thanks.

8
  • Doesn't the static field belong to the class definition, and not to any given runtime object of the class type? Reflection is a runtime mechanism; I doubt what you're proposing is possible. The closest thing I can think of is a Singleton, since a Singleton always refers to the same runtime instance. Commented Mar 14, 2013 at 23:38
  • but it might be possible to dynamically add a new field to the class. Commented Mar 14, 2013 at 23:40
  • 2
    I don't see how. Classes are a compile-time mechanism. Commented Mar 14, 2013 at 23:41
  • The compiler pulls static final fields into referenced classes as hardcoded constants so if your field is static final the answer is a resounding No. Commented Mar 14, 2013 at 23:42
  • 1
    no not final just static Commented Mar 14, 2013 at 23:42

2 Answers 2

5

You could do this during class load time using bytecode manipulation.

This is a very complex solution though, so I'd consider other options.

It also does not help too much to have a new field that is not known at compile-time, because you cannot compile anything against it. If you are going to use reflection to access it, you might as well use a Map in the first place.

Sign up to request clarification or add additional context in comments.

6 Comments

Anything is possible given enough time, money and programmer resources.
yeah but is it possible to do with a reasonable amount of effort and programming
yes, take a look at bytecode manipulation libraries. But again, why? How are you going to access these fields? You cannot compile against fields that a compiler does not know about.
reasonable: In your case, I doubt that. You may want to add code/functionality dynamically, but adding fields at runtime defeats the purpose of a statically typed compiled language. Maybe you want to use Jython or Groovy or JRuby or something.
For a certain class of use cases, you could look into AOP / AspectJ. It's basically a structured way of doing some kinds of metaprogramming in Java. (Including introducing entirely new members.) It's at least marginally less crazy than direct bytecode manipulation!
|
0

Java doesn't support metaprogramming or runtime programming in a way that is particularly nice or effective.

You could use a decorator pattern. You could pass the object that you want to add a static field to into a wrapper object. This wrapper would have the static field and the calls to the wrapper object would relate to the wrapped object.

If you could provide more details about the functionality you're looking for I could try to provide a better solution. You might be better off looking into another language that does support runtime programming if you absolutely need it to be done in that way.

2 Comments

i was just trying to see if it was possible to add fields to an object/class just for the sake of doing it, i enjoy doing things like this for fun
Understandable. It's a useful tool in some other languages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.