Java:在使用getter和setter访问另一个类的私有变量时出现NoSuchMethodError。

huangapple go评论69阅读模式
英文:

Java: NoSuchMethodError when using getters and setters to access private variables from another class

问题

  • 我正在理解 Java 中 getters 和 setters 的工作原理。我试图通过像 getName()、setName() 这样的方法访问其他类的私有变量(id、name),但是我的主方法无法检测到这些方法并报错:
error: Exception in thread "main" java.lang.NoSuchMethodError: 'void Employee.setName(java.lang.String)'
    at getterssetters_41.main(getterssetters_41.java:5)

这是我的主类:

public class getterssetters_41 {
    public static void main(String args[]) {
        Employee emp1 = new Employee();
        String anurag = "Anurag";
        emp1.setName(anurag);
        System.out.println(emp1.getName());
        emp1.setId(1);
        System.out.println(emp1.getId());
    }
}

这是包含我想要访问的变量和方法的类:

class Employee {
    private int id;
    private String name;
    
    public String getName() {
        return this.name;
    }
    
    public void setName(String n) {
        name = n;
    }
    
    public void setId(int i) {
        id = i;
    }
    
    public int getId() {
        return this.id;
    }
}
英文:

-I am understanding working of getters and setters in java. I am trying to access private variables(id, name) of other class through -methods like getName(),setName(). but my main method is not able to detect these methods and gives

>error: Exception in thread "main" java.lang.NoSuchMethodError: 'void Employee.setName(java.lang.String)'
> at getterssetters_41.main(getterssetters_41.java:5)

This is my main class:

public  class getterssetters_41 {
    public static void main(String args[]) {
        Employee emp1 = new Employee();
        String anurag="Anurag";
        emp1.setName(anurag);
        System.out.println(emp1.getName());
        emp1.setId(1);
        System.out.println(emp1.getId());
   }
}

>//This is class which have variables and methods I want to access

class Employee {
    private int id;
    private  String name;
    public String getName(){
        return this.name;
    }
    public void setName(String n){
        name = n;
    }
    public void setId(int i){
        id = i;
    }    public int getId(){
        return this.id;
    }
}

'

答案1

得分: 2

错误是不可能的,除非您已经执行了以下操作:

  • 您已按照添加这些getter/setter之前的方式编译了您的代码。
  • 然后,您添加了它们并(重新)编译了一切。但是,编译到一个新位置。或者,您只重新编译了主类,而源文件中的Employee已更改(但尚未重新编译!)。
  • 您现在正在运行由编译main之后__更新__的类文件,与编译Employee之前__创建__的Employee类文件一起。

解决方案:重新编译所有内容。

NoSuchMethodError只有在编译代码时存在方法,但在运行时不再存在时才会发生,即当您将不同运行的编译输出混合在一起时(或者,如果不是在您自己的代码内,您已针对某个库的版本X进行编译,但在运行时类路径上存在版本Y,或者X和Y都在类路径上,导致在运行时使用任意一个)。

英文:

That error is not possible unless you have done the following:

  • You have compiled your code the way it was before you added those getters/setters.
  • You then added them and (re)compiled everything. However, to a new location. Or, you have recompiled only your main class, with the changed (but not yet recompiled!) Employee source file on the sourcepath.
  • You are now running the class file created by compiling main after your update, with the Employee class file created by compiling Employee before it.

Solution: Recompile everything.

NoSuchMethodError can only occur if a method does exist when you compile your code but no longer exists at runtime - i.e. when you are mixing compilation output from different runs together (or, if it's not within your own code, you have compiled against version X of some library but at runtime version Y is on the classpath, or both X and Y are on the classpath resulting in an arbitrary one being used at runtime).

huangapple
  • 本文由 发表于 2023年5月29日 00:29:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定