Andrew T.
Andrew T.

Reputation: 4686

Modify a Static Variable when accessed

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.

Upvotes: 2

Views: 180

Answers (4)

user153
user153

Reputation: 146

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.

Upvotes: 0

Federico Piazza
Federico Piazza

Reputation: 31035

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.

Upvotes: 1

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477170

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).

Upvotes: 2

Philipp
Philipp

Reputation: 69683

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.

Upvotes: 6

Related Questions