private All Args Constructor in Lombok

1,320 views
Skip to first unread message

Sally

unread,
Sep 28, 2016, 8:00:07 AM9/28/16
to Project Lombok
Hello

I am using static factory method to create instances of class. Static factory method is using private all-args constructor. Is there any way to create private All-Args Constructor using lombok



public class X {

    private String a;
    private String b;
    private String c;
  

    private X(String a, String b, String c) {
        this.a = a;
        this.b = b;
        this.c = c;
       
    }

   
    public static X getInstance(String a, String b, String c) {
        return new X(a, b, c);
    }
}


Query : Any Lombok annotation to create All-Args constructor with private modifier ?

Martin Grajcar

unread,
Sep 28, 2016, 8:04:13 AM9/28/16
to project...@googlegroups.com
Using @AllArgsConstructor(staticName = "getInstance") you get it all: A private constructor and a public factory method.

You could also use e.g. access=AccessLevel.PROTECTED. This normally applies to the constructor, in case of factory method, it applies to to it and the constructor is private.

--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombok+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jonesong

unread,
Oct 2, 2016, 6:25:27 AM10/2/16
to Project Lombok
Hello, addition to Sally's question. Query : Any Lombok annotation to create All-Args constructor with private modifier ? Is it possible to add with @NoArgsConstructor ? because i'm using All-Args constructor and NoArgs contructor

public class X {

    private String a;
    private String b;
    private String c;
   

    private X(String a, String b, String c) {
        this.a = a;
        this.b = b;
        this.c = c;
        
    }

    public X () {
    }
}

Thanks
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombo...@googlegroups.com.

Martin Grajcar

unread,
Oct 2, 2016, 7:27:13 AM10/2/16
to project...@googlegroups.com
@AllArgsConstructor(access=AccessLevel.PRIVATE)
@NoArgsConstructor

should do, why not?


To unsubscribe from this group and stop receiving emails from it, send an email to project-lombok+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages