1

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!!!

6
  • 1
    -1 never read != never write Commented May 28, 2014 at 9:55
  • 1
    @PeterRader So do you down-vote everyone who makes a mistake? A +1 for me for a clear question and a code example that demonstrates the problem. Commented May 28, 2014 at 9:57
  • 1
    @Duncan, no i downvote questions only, not persons. I downvote questions that are founded in unrealistic situations like: When i balance a chair in magma, why hurts my neck? Give me a realistic situation for the code, and the yellow line will be disappear as well as the question disappears! Commented May 28, 2014 at 10:05
  • @PeterRader I think the OP has genuinely written this code and doesn't understand the warning. I don't think this is an unrealistic or hypothetical question. Commented May 28, 2014 at 10:14
  • 1
    @PeterRader I believe most answers below explain the core problem behind the warning. My biased view is that my own answer provides a few additional pointers about variable hiding. Anyway, let's leave the conversation here - it's quite fine if we disagree on down-votes. Commented May 28, 2014 at 10:26

6 Answers 6

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

0

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.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.