Reputation: 513
I'm trying to create a constructor inside an abstract class and then make two other classes extend it. But - I get this yellow line under some variable saying it never read locally, but if I delete it, I get compilation errors.
It looks like this:
public abstract class SizeFilters {
private double value; // this one never read locally
private int MIN_VALUE = 0;
public SizeFilters(double value) {
if (value >= MIN_VALUE) {
this.value = value;
} else {
throw new IllegalArgumentException(
"Value must be a positive number.");
}
}
And the class that extends:
public class GreaterThanFilter extends SizeFilters {
private double value;
public GreaterThanFilter(double value) {
super(value);
}
Should I ignore that warning or is there something I do wrong?
Thanks a lot!!!
Upvotes: 1
Views: 100
Reputation: 441
You shouldn't ignore the warning. Private member isn't visible to the sub-classes, so if you don't access it in the abstract class it won't be accessed at all. If you plan accessing it in the sub-classes, add a getter or make it protected.
On other hand, if the only purpose of the abstract class is sharing the member, I'd suggest you to avoid doing that. You should share functionality (methods), but not members alone.
Upvotes: 0
Reputation: 69399
In your subclass, you are hiding the variable defined in the superclass. This is because you've declared a field with the same name: value
:
public abstract class SizeFilters {
private double value; // this one never read locally
public class GreaterThanFilter extends SizeFilters {
private double value; // hides the field in the parent class
As a result, even if you use the value
field elsewhere in your subclass, the superclass field will always have a warning since the field is never read.
I suspect you need to remove your value
field from your subclass and provide a getValue()
method in your superclass to provide access to it. Or just make the field in your superclass protected
rather than private
.
Upvotes: 1
Reputation: 3333
I think you should make the double value
in SizeFilters
as protected
, and then delete the one defined in subclasses like GreaterThanFilter
. So you use the inherited one from your base abstract class.
As for your question, and as all other answers mentioned, it's because you never read the value attribute, you just set it. And the compiler, tells you, that it's never used
Upvotes: 0
Reputation: 1239
You are never read this variable. The only thing you do with it is giving it a value but you never read it and it's value.
Upvotes: 0
Reputation: 431
The reason you're getting the warning is most probably because it's never actually used apart from the constructor. If you're going to use it within the class later on (with a getter method or other way), ignore the warning.
Upvotes: 0
Reputation: 692181
The warning basically tells you that your class has a field, which is initialized, but is never used by any method of the class (and can't be used by subclasses since it's private).
So it's a hint that either your class lacks methods using the field, or that the field is useless and could thus be removed.
Upvotes: 0