Can we write a constructor for a global class?

If we want to write a constructor for a global class what we need to do?

global class MyWrapper {
    public static object state{get;set;}
    public String Request { get; set; }
    public String Variable1 { get; set; }
    public String Variable2 { get; set; }

    global MyWrapper(String req, String var1, String var2) {
        Request = req;
        Variable1 = var1;
        Variable2 = var2;

Can we define it like the above?


Yes you can choose to make specific constructors, methods and fields global. Your global constructor looks correct to me.

(You only need to use global when you want to make code within a managed package usable from outside the managed package.)

But bear in mind that once packaged, you will not be able to change the signature so use global sparingly.

| improve this answer | |
  • That last point is really important. It bears repeating. Use global sparingly! – Nick Cook Sep 5 '16 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.