0

I have interface :

public interface Myglobal 
{
public int Type =0;
}

Then I have class implement My interface like :

Public class A implements Myglobal 
{
Public class A ()
{
this.type=1; // here error because type final in interface
}
}

I want for evrey class implement the interface to change the value of variable type ... so how I can do it with java ?

sara
  • 168
  • 1
  • 1
  • 8

1 Answers1

0

I think you should use abstract classes to achieve what you're trying to do, interfaces can't have values like that that can be changed. They would also be static as you don't create instances of an interface.

I would also use getter/setter functions for working with the value.

public abstract class Myglobal 
{
    private int Type =0;

    public void setType(int type) {
        this.Type = type;
    }

    public int getType() {
        return this.Type;
    }
}

public class A extends Myglobal 
{
    public class A ()
    {
        this.setType(1);
    }
}
Ben Cummins
  • 498
  • 3
  • 6