-3

I have a class like:

public class TemplateFileResponse {
    private String path;
    private List<FileView> children;
}

I want to create an instance and set children is empty array. so what is the best way to do it?

Nguyễn VŨ
  • 81
  • 2
  • 5

1 Answers1

-1

You can create an empty list with the new operator:

public class TemplateFileResponse {
    private String path;
    private List<FileView> children = new ArrayList<>();
}

You may also want to initialize the path field, either in a constructor or inline, because otherwise it will be initialized to null by default.

I suggest that you read a tutorial about Java classes, constructors, methods, and instantiating objects to understand how all of this works.

Michael
  • 37,794
  • 9
  • 70
  • 113
Code-Apprentice
  • 76,639
  • 19
  • 130
  • 241