Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ?

What I have tried:

I read the java not allow ... but what I can to do to make evrey class implement this interface to change the value of variable in interface ?
Posted
Updated 17-Sep-16 10:37am
v2

 
Share this answer
 
As you have concluded
Type
is a constant that cannot be changed. Thus you have to create a interface method and then simply implement this in each implementing class like this:

Java
public interface Myglobal 
{
     public int getType();
}
 
public class A implements Myglobal 
{
     public int getType() { return 1; }
}
 
Share this answer
 
Comments
Member 12702368 17-Sep-16 13:09pm    
Ok ... thank you @Henrik jonsson

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900