-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Ũ
  • 161
  • 2
  • 6

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
  • 41,989
  • 11
  • 82
  • 128
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268