1

I can write Integer var {get; set;}

but can not Integer var {get; set;} = 0

or

Integer var = 0 {get; set;}

Is it impossible to init field with set-get at the same line?

4

1 Answer 1

5

You can set them in single line as well. You just need to follow the format. Which is

public class BasicProperty {
   public integer prop {
      get { return prop; }
      set { prop = value; }
   }
}

if you want to set a single element then can do this

public class BasicProperty {
       public integer prop {
          get ;
          set { prop = value; }
       }
    }

You can also make them public or private

public integer Z { get; protected set; }

So there is no such limitation that you can't use them in single line.

Apex properties

1
  • You provided 3 examples, and none of them initialized property, which is what author asked about
    – piezol
    Commented Nov 22, 2023 at 7:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .