Skip navigation

For a long time, I kept thinking “there is no point to a private variable if all you do is get/set it”

yeah yeah encapsulation encapsulation and whatever. But what if the get/set is just a pass-through? Surely its better in that case to use a public field.

HOWEVER. There is more to encapsulation

  • With the GET/SET, you remove “dependence” of your client on your field names. This means you have the OPPORTUNITY to change internally how you manage a variable. This makes it so your client (user of your class) DOES NOT NEED TO adjust his code, in case you need to change how a particular member is treated internally to your class

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: