Necessity of access modifiers for instance field with set methods

I am learning java. Now, it is always a practice in oop to make the fields in a class private and provide get and set methods as per the requirements to access or modify the variables. Now my question is that if you have a set method for a field that is declared private, the user can still change the value of the field even if it is invisible. So, why not make the field public instead of providing a set method?

Answers


It is always a practice in oop to make the fields in a class private and provide get and set methods as per the requirements to access or modify the variables.

Variables are best kept private (although you can keep it public). Not all variables need get and set (accessors and mutators) functions. It depends on use case. For example if I am holding the count of some sort, which is internal to the class, I would not provide a set(..) method to it. If your get(..) and set(..) methods do nothing more than returning and assigning, I would suggest you follow your company's coding guidelines. And if you have the option go for getters and setters.

Now my question is that if you have a set method for a field that is declared private, the user can still change the value of the field even if it is invisible. So, why not make the field public instead of providing a set method?

A very simple/trivial example here:

void setVar(string x){
    myPrivateVar = x;

    //log the change..
    writeToLog("myPrivateVar was updated to: " + x + " at <blah blah>");

    //inform others of the change.
    updateDependantConfiguration();
}

HTH ..


Try to read about encapsulation and you will understand the reasons better.


You are right - setters and getters can to change value of appropriate property. So, why do we need them?

I will try to enumerate some reasons here.

  • it is easier to maintain code. You can easily find who changes the property. Just look for all settter calls. Otherwise you can search for all places that refer to property and find 99% of places were you read the property and 1% where you write it.
  • It is easier to debug. You can put breakpoint in getter or setter depending on what are you looking for.
  • The fact that setter and getter are methods is used in byte code engineering and proxy creating in modern complicated frameworks.

Need Your Help

ASP.NET MVC Url display problem

asp.net-mvc asp.net-profiles html-encode

I have recently been experimenting with the profile features of ASP.NET. I am having trouble getting a "website" property to display correctly. For example, if the website I enter is:

Should a unit test rely on constants defined by the application?

unit-testing testing language-agnostic constants

Consider the following pseudocode. It is designed to determine whether a grade is a passing grade.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.