Tneuktippa
Tneuktippa

Reputation: 1695

What's the point of automatic properties that define both get and set in apex?

It makes sense to me why you might want to have a property like

public string var {get;}

to make it read-only, or

public string var {set;}

to make var write-only (although I'm not sure where that would be useful). But what does

public string var {get; set;}

get you that making var a bog-standard variable doesn't?

Upvotes: 0

Views: 660

Answers (2)

Adam
Adam

Reputation: 2605

Nothing really. I prefer the explicit:

public String myVar {get; private set;}

to simply omitting the set. It's really just a shorthand convention of the classic:

public String myVar
{
   get
   {
      return myVar;
   }
   set(String s)
   {
      myVar = s;
   }
}

When in Rome...

Upvotes: 1

Steven Herod
Steven Herod

Reputation: 764

It makes it visible to the VisualForce page.

public String var;

Won't let you get to the var variable from Visualforce.

I.e.

<apex:outputText value="{!var}">

will fail.

Upvotes: 0

Related Questions