0

Bean class /encapsulation means variable should be private & getter & setter should be public. Because of the reason data hiding.Anybody can't access this variable out side.

My Question is :`With the getter/setter method we can access/set variable from outside class.Then why we need to keep that variable private && how we can tell this is data-hiding?

Actually i like to get some explanation because , without any idea just i am doing project like this. Please if there are any mistake or unclear question, please forgive me.

Please anybody explain this.

Thanks in advance

3
  • 2
    silly example: your variable could be an int, but you could get and set a String value parsing it... in a transparent/incapsulated way Commented Apr 10, 2012 at 8:45
  • 2
    possible duplicate of Java Setter and Getter Commented Apr 10, 2012 at 8:46
  • 1
    We can using setter/getter as filter concept. For example if you want authorize anyone that call setter, you do this task into setter method. Commented Apr 10, 2012 at 8:51

3 Answers 3

2

Few reasons I just thought about:

  1. you control how a variable is being accessed, for example by not allowing invalid values like null to be set in the setter or even not allowing a setter at all for read only variables.
  2. you can change the structure of the variable e.g. its data type and keep the setter and getter definitions without any change, so it is easier for you to do internal change/refactoring without affecting all the users of the class.
Sign up to request clarification or add additional context in comments.

Comments

1

The getter / setter define a readable / writeable (or both) property of the class. The private field declares one possible internal representation of this property.

By using a property definition (getter and setter) you can define the access to this field or you can add constraints on the setter. In case of collections you can ensure to provide only an unmodifiable Versions.

Also you are able to delegate to delegate the value retrival to another class or object during refactoring without changing the collaborating objects. You are able to change the internal datatype, e.g. build data classes too.

So you encapsulate the internal state and provide access.

Remember not everey field needs getter and setter ;-)

Comments

1

1) By doing so, you are actually exposing the variables value but it encapsulates the way the value of the variable is calculated. With this, you can change the way the value of variable is calculated at any point of time, according to your requirement.

2) This way of representation helps "Transfer Object" Design pattern. What it says is that you can mold all the variables into single object and send it to client rather than sending each attribute value, every time when client asks for one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.