Just like stated in title, is it possible to implement such functionality in java? Thanks.

Suppose I have a class myClass.java, is it possible to achieve below functionality

public class myClass {

@Value("{myString}")
private static String myString;

static String myStaticMethod (final Stringp[] args) {
if args.length > 0 {
retrun args[0] + myString;}
}
}
1

There are 1 best solutions below

0
On

You can achieve this, BUT

  1. the @Value annotation needs to be a non-static field, and then you can pass it to a static variable after the injection happens (if this is Spring, you can listen to a life cycle event - or change the injection to happen in the constructor)
  2. you can only use your static method after the injection happens.

So altogether unwise, unless you are in full control of the application life cycle.