Inspectopedia
 
2024.3

Return of instance of anonymous, local or inner class

Warning
New
Last modified: 03 December 2024

Reports return statements that return an instance of an anonymous, local, or inner class. Such instances keep an implicit reference to the outer instance, which can prevent the outer instance from being garbage-collected. Any caller of a method returning such an instance might cause a memory leak by holding on to the instance returned.

Configure the inspection:

  • Use the Ignore returns from non-public methods option to ignore returns from protected or package-private methods. Returns from private methods are always ignored.