1

I've been using a Java book which has been excellent so far but it hasn't been to good at explaining the reason why we use setters and getter. I've searched other posts but none have really helped me specifically. I'll just post a program from the book and explain my problems with it.

   package lesson1;
public class GoodDog {
   private int size;

   public void setSize(int sz){
      if(sz > 10){
         size = sz;
      }
   }
   public int getSize(){
     return size;
   }
}

Second class:

   package lesson1;
public class GoodDogTestDrive {

   public static void main(String[] args) {
      GoodDog one = new GoodDog();
      one.setSize(15);
      System.out.println(one.getSize());
   }
}

This is my first post so if it doesn't come out as code then forgive me. Don't ask why the classes are named that, I had no imagination and just used the book's name.

Anyway, my understanding is that encapsulation (To me this basically means getters and setters) prevents the direct access to instant variables. The only valid reason for this is because some values of instant variables should not be allowed.

For example say we had int height; (Instant variable) We should not be able to say object.height = 0; ('object' being a random reference variable). In my little program it is simply a dog's height with a simply restriction. It's not meant to make sense but I'm just trying to make sense of this concept. Is encapsulation simply meant for programming firms when programmers need to use each others code to. say, make a game for example or is it different?

I watched a video also stating that it simply makes code easier to manage. However I still don't feel that I fully understand this concept. Can someone please explain this to me in simple terms. Please note that I'm a beginner and will not understand examples involving intermediate code.

Thank you for your help.

aliteralmind
  • 19,847
  • 17
  • 77
  • 108
Mr Spartacus
  • 127
  • 1
  • 10
  • the only role of getters setters i found was in java beans http://en.wikipedia.org/wiki/JavaBeans – user2837260 Jul 18 '14 at 00:49
  • haha I'm guessing I'm pushing it. Maybe just knowing what it is mainly used for would suffice :) Simplicity? Hiding code? Maybe both of these? – Mr Spartacus Jul 18 '14 at 00:50
  • you have specific built in tags that can save ur time if u have used proper getters/setters..just define like they should be and jsp will do the rest..u cant escape them in jsp but for normal programming you can go define functions any which way u want – user2837260 Jul 18 '14 at 00:51
  • 1
    http://stackoverflow.com/questions/16418571/what-is-the-use-of-encapsulation-in-java-i-can-able-to-change-the-property-valu see this – bumbumpaw Jul 18 '14 at 00:53
  • Niang - Thank you for the link, it was a great help. – Mr Spartacus Jul 18 '14 at 01:02
  • Thanks for the help Mr crassus – Mr Spartacus Jul 18 '14 at 01:03
  • possible duplicate of [Why use getters and setters?](http://stackoverflow.com/questions/1568091/why-use-getters-and-setters) – elixenide Jul 18 '14 at 02:21

2 Answers2

5

If I understand what you're asking, you can think of encapsulation as the practice of utilizing data-hiding features of a programming language. One of the core principles of object orientation is that objects contain both data and methods to operate on that data. Encapsulation is the general internalization of the data so that only an object's own methods can modify the object's state.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 1
    Also, considering an object's exposure for use by other objects either in process (e.g same JVM) or out of process (e.g indirect acess via a web service), the above concepts are important as it ensures a well defined contract to developers. Basically, a self-contained unit, if you will encouraging team collaboration in a layered manner. – Rai Jul 18 '14 at 01:02
1

Using private variables with getters and setters provides a number of advantages:

  1. Allows restriction of access to data that might be sensitive
  2. Allows you to take action when a value is set; e.g. Incrementing a counter
  3. Allows for "lazy initialization"; you can wait to set the value of a variable until the get method is called, which can improve performance
  4. Information Hiding - There might be some sort of implementation detail involved in setting or getting a value that isn't necessary for the user of the class to know
Caleb Brinkman
  • 2,489
  • 3
  • 26
  • 40