java - 我如何能够在实现该接口的类中更改接口变量值
问题描述
我创建了一个界面,如下所示:
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");
}}
但是规则说我不允许更改接口变量的值。有人可以告诉我我做错了什么吗?
解决方案
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
推荐阅读
- sql - 为什么 soundex 返回不相关的结果
- javascript - 带括号的 javascript 和 html
- rabbitmq - RabbitMQ - 无法删除队列,因为找不到队列
- java - 过滤具有特殊字符的列族
- php - 提交带有特殊字符的表单时出现 403 禁止错误
- javascript - 页面加载时表单中的随机文本
- struts2 - 使用离子原生高级 http 时会话 Cookie 丢失
- php - preg_replace 使第一个字符大写而不使用像 ucwords() 这样的内置函数
- c++ - 运行搜索数组
- rabbitmq - Rabbitmq 无需重启即可重新加载/刷新新证书