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.
-
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. – Robert Harvey Mar 14 '13 at 23:38
-
but it might be possible to dynamically add a new field to the class. – Josh Sobel Mar 14 '13 at 23:40
-
2I don't see how. Classes are a compile-time mechanism. – Robert Harvey Mar 14 '13 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**. – Boris the Spider Mar 14 '13 at 23:42
-
1no not final just static – Josh Sobel Mar 14 '13 at 23:42
-
You will probably need to decompile java.dll to see what it is doing. It may be possible. I am guessing you want to override public/protected fields in parent classes? – BevynQ Mar 15 '13 at 00:04
-
@BevynQ: You cannot override static fields no matter what you do. – Thilo Mar 15 '13 at 00:07
-
@Thilo: [from jls](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.1) Not overriding in the polymorphic sense, and of very restricted use I would think. – BevynQ Mar 15 '13 at 00:35
2 Answers
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.
-
3Anything is possible given enough time, money and programmer resources. – Robert Harvey Mar 14 '13 at 23:41
-
yeah but is it possible to do with a reasonable amount of effort and programming – Josh Sobel Mar 14 '13 at 23:42
-
1yes, 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. – Thilo Mar 14 '13 at 23:42
-
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. – Thilo Mar 14 '13 at 23:44
-
"You may want to add code/functionality dynamically": Good example: New Relic's Java monitoring agent. It injects all kind of metrics gathering code into your existing Java classes. – Thilo Mar 14 '13 at 23:45
-
2For a certain class of use cases, you could look into AOP / [AspectJ](http://www.eclipse.org/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! – millimoose Mar 15 '13 at 00:00
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.

- 151
- 1
- 13
-
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 – Josh Sobel Mar 15 '13 at 00:59
-