Pregunta

I declared the following interface:

public interface SQLSyncAble
{
    boolean modified = true;
    long id = -1;

    static HashMap<String,SQLAction> SQLModifiers = new HashMap<String,SQLAction>();
    static Field[] SQL_Object_Fields = null;
    static DataType[] TransferDataTypes = null;
}

In other classes, where i would to like set a object which implement this interface, i get compilation error:

someobject.id = 10  // The final field SQLSyncAble.id cannot be assigned.
                    //The static field SQLSyncAble.id should be accessed in a static way.

LOL what? "id" and "modified" is not "static final", i let see the compiled .class file (im inspected in a Java assemly editor, and in eclipse), and really! Every time (does not matter by eclipse or java) int the complied class all of fields will have "static final" modifier. But why?

If i modify this interface to "abstract class" the problem instantly disappears.

...But it is only half solution, java allow only one of superclass.

Why complied my interface with undesirable modifiers?

¿Fue útil?

Solución

By definition, all fields of interfaces are marked static final.

You should have a class surrounding the interface if you need non-static, non-final members.

Otros consejos

You can't change final variable when ever, after you initialize it.

all field we are define in interfaces implicitly public, static and final. again you were assign a value in the interface and you try to change in later, So you can't do that.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top