0

I know it has been asked before, but I still am not truly getting it.

People say it is about encapsulation, to protect the fields from outside classes from being accessed? But what's the point of protecting the fields if you are using a get/set methods to change and access the fields anyways?

People also said using get/set methods, you can have the flexibility to add more logic into the methods. I agree, but what happens if your program will never require such a thing? In that case, can you just still declare the field as public instead of get/set method?

BeLambda
  • 887
  • 1
  • 10
  • 19
  • Well, the point is you don't know that your program never requires any additional logic. So you have to add setters/getters in case you need them. – markspace Feb 23 '16 at 04:33
  • Suppose if you want a variable only as a `return` something to caller method then you should implement only `getter` Not `setter`. There are so many other scenarios may be there, you can thought of.. – Vikrant Kashyap Feb 23 '16 at 04:34
  • http://stackoverflow.com/questions/11071407/advantage-of-set-and-get-methods-vs-public-variable – Ashish Feb 23 '16 at 04:35
  • There are multiple Varients or taste or `design Pattern` used by multiple programmers now a days. So Implement as per your need. – Vikrant Kashyap Feb 23 '16 at 04:35

3 Answers3

0

It is my understanding that if you make your fields public, other programs running on the same machine can access and edit the data unchecked (Someone correct me if I am wrong). Then there is the possibility of the program trying to store a String object into an address that is supposed to point to an Integer, resulting in an exception that could potentially be a security flaw.

The main idea is that if someone else uses your class as part of a bigger program, the set method can include validity checking and exception handling so as to protect them from having to deal with it.

Example:

private int value;
public void setNonNegativeValue(int newValue){
    try{
        if (newValue >= 0){
            value = newValue;
        }
    } catch (Exception e){
        //handle it here
    }
}

Now obviously this method isn't going to result in needing Exception handling, but you see how more complicated ones could.

In this case, value can only be a positive number because the setter method checks for that. However, if value was public, any class could just do myClass.value = -1; This would be a problem if you needed value to be positive

Jonah Haney
  • 521
  • 3
  • 12
0

public fields mean every one can access and modify them , but having private fields does not mean you have to declare its setter/getter every time it may depend upon your requirement, for suppose there is such field you want user to get its value but not to change its value. this is all data hiding depending upon your business.

Sindhoo Oad
  • 1,194
  • 2
  • 13
  • 29
0

You should not set your field as public. This is encapsulation in java. Others doesn't know how the values are populated in to the private variables, they may change it from several places, atlast it become a huge mess. But they have access to some method which is public. I didn't say Getters/Setters.

So up to your point Getter/Setter . Don't use that if you follow java encapsulation. This is same as public variable then (May be some more logic added to it, but user can alter that using setter).

What you have to do is, expose public method in the same class, do whatever manipulation you want to do with instance variables and return only the required results that user need to know.

Eg: Take a class CocaCola . You write a method createCola(). inside that you create the cola and return the result. User doesn't need to know the ingredients of Cola. If you create Getter/Setter otr make the ingredient public that is worse.

These are just standards or best approach in java, that experts suggest. So if you don't want to follow, you don't need to .

Refer:

Java Getters/Setters are evil

Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64