0

I know that interface can have only static and final values implemented inside it.. But is there any loophole by which I can change the value of a variable using interface?? The question may be absurd, but I m helpless since its my requirement. Here is the example piece of code..

public interface I {
    int val = 1;  
    int changeValue();

}

Class A implements I{
    int changeValue(){
        val = 2 ; 
        return 0;
    }
}

How to change the value of 'val' using interface? Can I do something similar to:

val = changeValue();

Is there anything equivalent to do this functionality in an interface?

Sam
  • 86,580
  • 20
  • 181
  • 179
Vikram
  • 2,042
  • 3
  • 14
  • 15
  • 1
    Interface variables are by default `public static final` and methods are by default `abstract public`. You can't use the `private` or `protected`modifier within an interface anyway. The sole purpose of an interface is to achieve publicly defined protocol behaviour. – Lion Oct 19 '12 at 23:13
  • Thank You @Lion .. please see my comments below, in answer – Vikram Oct 19 '12 at 23:16

2 Answers2

4

You cannot. Interface variables are static and final by default.

A final variable is a variable that cannot be changed during the life of the object.

A static vairable is a class variable - it means there is only one value of it for all instances of the class (or interface in this case).

Thus - you only have one value for I.x - and this value cannot be changed.


What you might want to do, is define methods in your interface:

int getVal();
void setVal(int val);

And make the implementing classes implement the methods - so you will be able to use the variable with the getVal() and setVal() methods.

amit
  • 175,853
  • 27
  • 231
  • 333
  • Variables in interfaces also are public by default. – Luiggi Mendoza Oct 19 '12 at 23:07
  • @LuiggiMendoza: To be even more exact, everything in an `interface` is `public` – amit Oct 19 '12 at 23:08
  • Thank You.. My actual requirement is such that I want to reuse, "Future>" in two methods of one class.. In 1st method, the value of "Future> changes and its being used back again in 2nd method.. I must not pass the values as parameter too. That is the reason why I selected Interface.. Now that knowing, its not feasible I have to think of some other option – Vikram Oct 19 '12 at 23:13
  • @amit , can you kindly brief a little bit about your getVal() and setVal() way of changing the value of 'val' ?? – Vikram Oct 19 '12 at 23:22
  • @asvikki: It is not changing the value of `val` - it will let you define a new variable in each implementing class - but not change an interface variable - that cannot be done. – amit Oct 19 '12 at 23:27
  • Clarification: "let you define..." really means - will let you a single API for changing/reading a newly added variable in each implementing class - but never the field that was declared in the interface. – amit Oct 19 '12 at 23:34
0

You cannot do this for an interface. However, it is possible to modify a static final variable in a class:

public abstract class I {
    static final int val; 
    static
    {
        val = 1;
    }
}

import java.lang.reflect.*;

public class NotFinal
{
    public static void main(String args[]) throws Exception
    {
        System.out.println(I.val); // Before: 1     

        Field field = I.class.getDeclaredField("val");
        field.setAccessible(true);
        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
        field.set(null, 2);

        System.out.println(I.val); // After: 2
    }
}

Output:

1
2

Note that this does not work if you assign the value in the declaration, i.e.

static final int val = 1;

because the compiler treats this differently (as a constant) - see this answer.

Community
  • 1
  • 1
DNA
  • 42,007
  • 12
  • 107
  • 146
  • Looks interesting .. Thanks. I ll give a try with this – Vikram Oct 19 '12 at 23:25
  • I don't actually recommend this approach - I merely point it out for academic purposes ;-) There is almost certainly a better way, but you'd need to explain a little more about what you are trying to achieve, perhaps in a new question. – DNA Oct 19 '12 at 23:26
  • Ok, got it :) can you recommend something for my query, U can see that in the first answer comments.. – Vikram Oct 19 '12 at 23:29