The value of some fields is not used when i add private method

北城以北 提交于 2020-08-11 18:39:07

问题


I'm new at Java and I'm attempting on my very first coding, but i hit a bump as in if i type private before data types it will say

The value of the field Teacher.tea_id is not used
The value of the field Teacher.name is not used

Nevertheless, if i removed "private" method then the error also was solved, so i wonder is the method automatically converted to public when i removed "private"? And why error exists with private?

Btw, i also have another class namely "score", i'm not sure if i set a variable like sum which represents the sum total of several other variables, can i just continue use it in other classes such as the teacher in this case? But of course they are in the same package.

FYI, the teacher class looks like that:

public class Teacher {
 private int tea_id;
 private String name;
Score Score;

public Teacher(int tea_id, String name)
{
this.name = name;
this.tea_id = tea_id;
Score = new Score();
}
public void input(float num, int course){}
public void input(float[] score){}
public void check(){}

public float getSum(){return Score.sum;}
public float getAverage(){return Score.average;}
}

Hope i have clearly delivered my problem to you, and will clarify further if needed.


回答1:


IDE give syou warning because those variables cannot be used/accessed out of class whatsoever. Yeah you initialize them within constructor but then they're useless in IDE opinion. IntelliJ for example has two warnings to separate situation with variable not assigned and not used at all and situation when variable assigned and not used. When no access scope is defined there is default package scope. But it won't remove warnings. To avoid those warnings create getters and setters for those variables. Code with getters/setters is as follows:

package com.java.stackoverflow;

public class Teacher {

    private int tea_id;
    private String name;
    Score Score;

    public Teacher(int tea_id, String name) {
        this.name = name;
        this.tea_id = tea_id;
        Score = new Score();
    }

    public void input(float num, int course) {
    }

    public void input(float[] score) {
    }

    public void check() {
    }

    public float getSum() {
        return Score.sum;
    }

    public float getAverage() {
        return Score.average;
    }

    public int getTea_id() {
        return tea_id;
    }

    public void setTea_id(int tea_id) {
        this.tea_id = tea_id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}



回答2:


If a variable is private, it can only be accessed from within that file. If you have a private variable that is not accessed within the file, it generates a warning, since it doesn't do any good and is probably a mistake (you either meant to use it and didn't, or you forgot to delete it).

If you remove "private", it is now visible within the package. IDEs will often still produce the warning since they assume they have all files in the package accessible to them. But if you change it to "public," it's accessible to any code that includes the code you are working on. If you are writing, say, a video game, that supports mods/plug-ins to be written, you may have a public variable that some future mod can use. This is why your compiler does not generate a warning in this case: because you may have intended some code somewhere else to use it.



来源:https://stackoverflow.com/questions/25757033/the-value-of-some-fields-is-not-used-when-i-add-private-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!