2

From what I know about java I don't think this is possible but I would like to pose this question to people with far more knowledge than I do.

If I have a static variable say,

public static String NAME = "james";

Is there any way, through reflection or otherwise, to create a listener for said variable such that if someone else calls:

ClassName.NAME

It can be modified before they get the result, so I can change the value of NAME so that it equals "simon" instead?

To be clear, this code base is not my own and I can not change the variable to instead use getters and setters. I know that would make this much simpler, but that isn't an option unfortunately.

7
  • 2
    No, not with field access and not with plain Java. Look into AspectJ. Commented Jul 9, 2014 at 15:23
  • 1
    Make the variable private and provide public getter and setter functions. Commented Jul 9, 2014 at 15:24
  • @KevinWorkman I am working with a code base that is not my own, I can't change the variable to be a method without breaking everyone else's code unfortunately. Commented Jul 9, 2014 at 15:25
  • @AndrewT. Then you're mostly out of luck. You might set up a Timer that checks the value of the variable every so often, but you won't be able to be notified immediately or know who did the accessing. Commented Jul 9, 2014 at 15:29
  • 1
    @CommuSoft Right you are. Like I said, he's mostly out of luck. Commented Jul 9, 2014 at 15:31

4 Answers 4

6

No, you can't. That's one of the reasons why you shouldn't use public variables. Always use private or protected variables and access them through a getter-method like static public String getName(). That way you can put any logic into the getter you want.

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

Comments

2

There is a saying in computer science that you can achieve anything by "Another level of indirection", thus use an access method:

public class Foo {

    private static String name;

    public static String getName () {
        String result = name;
        //do a lot of other things.
        return result;
    }


}

Otherwise, I think it is not possible. You could rewrite the byte code: such that every call to the item is replaced by first doing some other things. But this is very complex.

If it's not your own, you can't do it, unless with an enormous effort (rewriting bytecode).

Comments

1

You can do some tricks to achieve this, you can't modify it directly but you can use public static method to access that variable.

On the other hand, I agree to Sotirios Delimanolis you can use aspects to achieve this.

You can create a pointcut for that variable to modify it before, after or around it.

For me the best way to do this is to create a private static variable and access it through a public static method, then use aspects to access to this method and apply the pre/post logic for your needs.

2 Comments

I don't know much about pointcuts or aspectj, but without changing the static variable to private, and accessing it via methods, could I use a pointcut on it to modify it when someone attempts to read it?
@AndrewT. I never used aspects to modify a static variables, I've always used with methods. But you can have both things a public static variable and a public static method to use it as proxy to modify the variable using an aspect. So you combine both needs
0

You can edit the value of static variable using reflection.

public class LoadClass {
    public static String name="James";
    public void disp(){

    }
    public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
        Field field = LoadClass.class.getDeclaredField("name");
        System.out.println(field.get(new LoadClass()));
        field.set(new LoadClass(), "simon");
        System.out.println("get field value " +field.get(new LoadClass()));
        System.out.println("After change : " + name);
    }

}

Not sure if this answers your question.

2 Comments

Not really. It's a public static variable I can change it very easily, it's changing it at a very specific point that is hard. If you can show me a way so that I can change what that variable returns specifically when it is accessed using reflection, that would help me a lot.
Yup.. you are right.we don't need reflection api, we can directly access & change the value. But since Andrew wanted a solution with reflection, i posted this.

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.