Shaurya
Shaurya

Reputation: 144

Lombok And Java Static Constructor Object Creation

Official document says that I can create object by using following approach:

 @Data(staticConstructor = "of")
class Foo<T> {

    private T x;
}

you can create new instances of Foo by writing: Foo.of(5); instead of having to write: new Foo(5);

This is a sample method to learn lombok.

package com.lombok.first;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.val;
@Data public class DataAnnot {
          @Getter @Setter private String name;
          private final int salary;
          
          
          
          @ToString(includeFieldNames=true)
          @Data(staticConstructor="of")
          public static class inner<T>{
          private T tally;
         }
          
          
     public static void main(String agrs[]){
     DataAnnot d= new DataAnnot(8);
     
     System.out.println(d);
   }
}

but when I add the line

 val obj= inner.of("object"); 
 System.out.println(d);

Eclipse flags error. What am I missing here? Perhaps I need to refresh my generics concepts, but how can I create an object of "inner" class?

Upvotes: 0

Views: 9222

Answers (1)

Roel Spilker
Roel Spilker

Reputation: 34452

Maybe you should use val obj = DataAnnot.inner.of("object");?

I cannot tell for sure since you didn't specify where you added the code snippet.

Upvotes: 2

Related Questions