0

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.

leppie
  • 115,091
  • 17
  • 196
  • 297
Vivi Xu
  • 131
  • 2
  • 5
  • 12
  • Is the version of the class you posted before or after you removed the `private` modifiers? I assume it is before. – isomarcte Sep 10 '14 at 03:55
  • Yes, it is before, i didn't make any change except for the private modifiers. – Vivi Xu Sep 10 '14 at 04:20
  • possible duplicate of [Warning message: The value of the field CancelAnAlarmActivity.mDetailsButton is not used](http://stackoverflow.com/questions/22478523/warning-message-the-value-of-the-field-cancelanalarmactivity-mdetailsbutton-is) – Joe Sep 12 '14 at 10:26

2 Answers2

3

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;
    }

}
  • Thanks for your detailed explanation. I set the tea_id and name as new variables and it also works fine. – Vivi Xu Sep 10 '14 at 09:30
0

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.

djechlin
  • 59,258
  • 35
  • 162
  • 290
  • But in this case I'm just trying to set new variables tea_id and name within this class(also called "file"?), and it also concerns about the use of modifiers(public, private)? – Vivi Xu Sep 10 '14 at 07:59