File: StaticAssignmentInConstructor.md

package info (click to toggle)
error-prone-java 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 15,076 kB
  • sloc: java: 171,398; xml: 1,459; sh: 34; makefile: 7
file content (12 lines) | stat: -rw-r--r-- 550 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
Assigning to a static variable from a constructor is highly indicative of a bug,
or error-prone design.

Common reasons are:

1.  The field simply should be an instance field, and there's a bug.

2.  An attempt is being made to lazily initialize a static field. In this case,
    first consider whether lazy initialization is necessary: it often isn't. If
    it is, doing it from a constructor is very hairy: the static field could be
    accessed from a static method before the class is even initialized. Consider
    using a memoized `Supplier`.