2

Normally in java bean classes we are declaring variables as private. Anyhow we are declaring setter and getter methods as public. Then we are able to get and set the value of property. So what is the use of declaring variable as private here? What happen if I declare as public?

Thanks in advance..

SimonC
  • 6,590
  • 1
  • 23
  • 40
samba
  • 2,263
  • 2
  • 13
  • 13
  • 2
    And who said that getters and setters are good ? http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html – Nir Alfasi Apr 23 '13 at 05:36
  • 1
    Check out encapsulation. Basic purpose is to protect the data. – Sudhanshu Umalkar Apr 23 '13 at 05:36
  • i studied in scjp book – samba Apr 23 '13 at 05:37
  • 1
    It's a concept based on [Encapsulation](http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29) where by the object provides methods that interact with the data. It prevents the modification of the data that the object is managing – MadProgrammer Apr 23 '13 at 05:37
  • how we are protecting here – samba Apr 23 '13 at 05:37
  • 2
    please give valid reason before downvote .please – samba Apr 23 '13 at 05:38
  • @samba We are protecting the data because the only way to interact with it is through the methods provided. Otherwise any one could change the values of the data without the knowlege of the object, possibly placing it within an invalid state – MadProgrammer Apr 23 '13 at 05:39
  • @MadProgrammer actually my doubt is any how we are able to access the variables through methods – samba Apr 23 '13 at 05:39
  • I am sorry if it is silly question for you.I dont know really – samba Apr 23 '13 at 05:40
  • @samba Only the class that declares the variables can access them. This means that any classes that might extend from this base class must use the methods to access the values as well, protecting the variables from inappropriate manipulation – MadProgrammer Apr 23 '13 at 05:42
  • @MadProgrammer thank u so much no i understand some thing.I will read about encapsulation – samba Apr 23 '13 at 05:43
  • 1
    if you keep your variables public .other class can directly change the values or read values. with variable private and Setter and Getter to access it allows you to validate the the new value before changing the data..hope it make sense – rahul maindargi Apr 23 '13 at 05:54

4 Answers4

9

That is the purpose of encapsualtion.

Consider having an age property and you want to have some control over it. If you expose your field simply as public, you can just access it externally and do whatever you want with it. Then, checking that the age is valid can be a little bit of a hassle.

That being said, with a setter method, you could, for instance, have a verification mechanism in place. This provides you with control over how and when is your variable accessed and changed.

The same applies for getter methods. Imagine you have some internal structure you do not want to expose, say, you use enumerations internally. However, you do not want to show this outside your class. So for instance, in your getter you yield the string version of whatever value you want to yield.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • +1 also consider having the age calculate from a `Date` value instead, the property is `age` is inferred from an internal state ... as a possible example ;) – MadProgrammer Apr 23 '13 at 05:41
  • @samba: You're welcome. MadProgrammer's example is also very valid and has more realistic implementations. – npinti Apr 23 '13 at 06:32
1
  1. You can put validation before setting value to the instance variables, like this:

    class HeightBean {
    
      private int height;
      public int getHeight() {
       return height;
      }
      public void setHeight(int height) {
      if(height<0)
          height=0;
      this.height = height;
      }
    }
    
  2. Most of the frameworks use bean setters and getters to access data.For example,

      <jsp:useBean id="name" class="java.lang.String" scope="session">
      </jsp:useBean>
      <jsp:getProperty property="bytes" name="name"/>
    
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
0

It offers a choice. You may or may not provide getters and setters.

For instance it is a common design choice to provide only getters in case of immutable objects.

Rule of thumb: Use as less visibility as you can get away with.

prashant
  • 1,805
  • 12
  • 19
0

The major reason for the one can be:

Restricting read and write accesses, and immutability:

  1. If I want to make an element as read-only. I would expose the getter as public and

  2. I can have only a setter, making the property configurable and any validation if I want to do while setting the value.

  3. A getter can make a defensive copy.

  4. You can have listeners associated, to notify any value change in setter method.

  5. If one field depends on another, you can put that logic in setter method, else you will always have to write a boiler-plate code for managing that, atleast a function call would still be required. That can be moved to setter.

But having a public attribute instead of setters and getters does not give this much flexibilities. You cannot do 1 to 5 without writing extra functions and calling overheads.

Himanshu Bhardwaj
  • 4,038
  • 3
  • 17
  • 36