Member Variables of an interface must be final... Why?

18,764

Well for a member variable, i think it is must to be static as the object cannot be created for the interface so to access the member variable one needs to have it static and access it through class.

Share:
18,764
AnkitChhajed
Author by

AnkitChhajed

Updated on June 04, 2022

Comments

  • AnkitChhajed
    AnkitChhajed almost 2 years

    I have a question in my mind, Why can't be a member variable in Interface be a non constant.. The logic of being static stood right in my mind that if one needs to access the variable of Interface then it is must for it to be static as we cannot create the instance of the Interface but Why the need of final arises?? The below code shows how the interface member variables are made static final even though we dont mention it by default....

    interface inter{
    
           int a=10; // It becomes final static by default
    
           public void interFunc();
    } 
    
    class cls implements inter{
    
           public void interFunc(){
    
               System.out.println("In Class Method WITH a's Value as --> "+a);
           }
    }
    
    class Test{
    
           public static void main(String[] args){
    
               inter in= new cls();
    
               in.interFunc();      
               }
    }
    

    Thanks in advance !!!