How to fill all the fields in the class?

前端 未结 5 1638
时光说笑
时光说笑 2021-01-12 16:40

Suppose I have a class that has a lot of different fields. This class is a DTO and for testing purposes, I do not care about actual values, just it exists. Is there any tool

5条回答
  •  有刺的猬
    2021-01-12 17:37

    If you are using primitives ,they will be automatically set to their default values. In case of Wrapper calss, if you do not care about actual values, you might leave them to null. You can throw a NullPointerException if they are accessed without initializing them.

    For populating it in the list, the simplest way should be create a object of class and adding objects to the list.

    class DTO
    {
      int a;
      String b;
      float c;
     DTO (int a,String b,float c)
       {
         this.a=a;
         this.b=b;
         this.c=c;
       }
    public static void main (String args[])
    
      {
          List  list = new ArrayList();
          DTO o = new DTO (1,"Test",11.3f);
          list.add(o);
       }
    }
    

    Printing the list and overriding toString() should display the values.

提交回复
热议问题