This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 267988 - Applying a "Field Can Be Final" Hint yields to uncompilable code due to multiple assignments to the same field
Summary: Applying a "Field Can Be Final" Hint yields to uncompilable code due to multi...
Status: NEW
Alias: None
Product: java
Classification: Unclassified
Component: Editor (show other bugs)
Version: 8.1
Hardware: PC Windows 7
: P3 normal (vote)
Assignee: Dusan Balek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-09-13 08:27 UTC by johannes.geiger
Modified: 2016-09-13 08:27 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description johannes.geiger 2016-09-13 08:27:20 UTC
Consider the following minimal examples:

1. The field "value" is correctly annotated with "Field value can be final" as it is initialized in both constructors.

    public class Example1FieldCanBeFinalCorrect {

        private String value;

        public Example1FieldCanBeFinalCorrect() {
            this.value = "";
        }

        public Example1FieldCanBeFinalCorrect(final String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }
    }

2. The field "value" is *not* annotated despite being initialized in both constructors. Having the Hint in this case would be a nice add-on but its absence is no error.

    public class Example2WithoutHint {

        private String value;

        public Example2WithoutHint() {
            this("");
        }

        public Example2WithoutHint(final String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }
    }

3. The field "value" is annotated incorrectly as the first constructor already calls the second constructor where value is initialized and assigns to value afterwards so that the final modifier leads to a compile-time error. I assume the "Field [...] can be final" hint only uses the field initialization in constructors directly used and does not traverse constructor chaining to collect all possibilities despite the compiler doing so afterwards. Having the Hint in this case is obviously erroneous.

    public class Example3FieldCanBeFinalIncorrect {

        private String value;

        public Example3FieldCanBeFinalIncorrect() {
            this("");

            value = "";
        }

        public Example3FieldCanBeFinalIncorrect(final String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }
    }