Skip to content

Latest commit

 

History

History
25 lines (20 loc) · 846 Bytes

AccessorClassGeneration.md

File metadata and controls

25 lines (20 loc) · 846 Bytes

AccessorClassGeneration

Category: pmd
Rule Key: pmd:AccessorClassGeneration


Instantiation by way of private constructors from outside of the constructor’s class often causes the generation of an accessor. A factory method, or non-privatization of the constructor can eliminate this situation. The generated class file is actually an interface. It gives the accessing class the ability to invoke a new hidden package scope constructor that takes the interface as a supplementary parameter. This turns a private constructor effectively into one with package scope, and is challenging to discern.

Example:

public class Outer {
  void method() {
    Inner ic = new Inner(); // Causes generation of accessor class
  }

public class Inner { private Inner() {} } }