Skip to content

Collection fields are unnecessarily initialized to "null" #1346

Closed

Description

@saberya

GenerationConfig has a option of "isInitializeCollections",
if it is true,
the result is

private List<Foo> fooList = new ArrayList<>();

if it is false
the result is

private List<Foo> fooList = null;

How can i get the result

private List<Foo> fooList;

Activity

  1. unkish commented on Dec 2, 2021

    @unkish
    Collaborator

    Hi

    private List<Foo> fooList; is equally same as private List<Foo> fooList = null;

    Please see JLS 4.12.5. Initial Values of Variables

    For all reference types (§4.3), the default value is null.

  2. saberya commented on Dec 2, 2021

    @saberya
    Author

    @unkish
    but

    private List<Foo> fooList = null;
    

    is not what i expect.
    Beacuse the java Class with this kind of expression is not a good habit to definition a field.

  3. unkish commented on Dec 2, 2021

    @unkish
    Collaborator

    I'm sorry but I don't understand why is that a problem or what kind of problem are you trying to solve

  4. saberya commented on Dec 2, 2021

    @saberya
    Author

    @unkish
    I use this tool to generate original pojo in a project before developing,

    private List<Foo> fooList = null;
    

    this kind of code is not friendly

  5. unkish commented on Dec 3, 2021

    @unkish
    Collaborator

    At present it's not possible to get the described result without either:

    • making changes to generator
    • overriding DefaultRule with custom one
  6. joelittlejohn commented on Dec 3, 2021

    @joelittlejohn
    Owner

    Not sure when this began happening, but I agree it would be nice to remove it. It's annoying noise.

    Feel free to submit a PR for this.

  7. changed the title [-]How can i generate collection fileld without initialized value and "null"[/-] [+]Collection fields are unnecessarily initialized to "null"[/+] on Dec 3, 2021
  8. modified the milestones: 1.1.3, 1.1.2 on Dec 6, 2021
  9. joelittlejohn commented on Dec 6, 2021

    @joelittlejohn
    Owner

    Closed by #1347

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions