首页 > 解决方案 > 我如何能够在实现该接口的类中更改接口变量值

问题描述

我创建了一个界面,如下所示:

public interface CalculatorInterface 
{ 
      int x=10; int y=15; int z=x+y; 
      public void add1();
}

然后我创建了一个实现它的类。该类如下所示:

public class AdvClass2 implements CalculatorInterface {

    public static void main(String[] args) {
       int x=50;
       System.out.println("X value is" +x);
    }

    @Override
    public void add1() {
        System.out.println("I am in Add Method");       
    }}

但是规则说我不允许更改接口变量的值。有人可以告诉我我做错了什么吗?

标签: java

解决方案


Variables in interface are by default static final ( you can call it as static constant ) variables ,so you can assign value to it only once , it's value cant be changed afterwards.

check this site for final keyword - https://www.javatpoint.com/final-keyword


推荐阅读