首页 > 解决方案 > 我的带有 final 关键字的 java 代码有错误

问题描述

// final keyword usage 
package workarea;
    
final class demo {
        
    final int x=10;
    ////compile time exception here because ‘x’ is final type
    System.out.println("hello modified x value is:"+ x);
    final void m1()
    {
        int x=2;
        System.out.println("hello modified x value is:"+x);
    }

    void m2()
    {
        System.out.println("hello modified m2 x value is:"+x);
    }

    public static void main(String abc[])
    {   
        demo df=new demo();
        System.out.println("welcome");
        df.m1();
        df.m2();
    }
}

这是我的代码,错误是:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

    at workarea.demo.main(demo.java:24)

错误消息是针对提​​到 main 的代码行 帮助我更正此代码

标签: javacompiler-errorsfinal

解决方案


System.out.println("hello modified x value is:"+ x);

如果您删除此行代码,您的应用程序将成功运行


推荐阅读