6 Replies Latest reply: Jun 2, 2008 12:32 PM by 807601 RSS

    Why do we declare the variables private in a bean

    800304
      Hi,
      when we create a bean in the MVC architecture why do we declare the variables private. Also when do we use the access specifier private.
      Regards,
      Prashant
        • 1. Re: Why do we declare the variables private in a bean
          807601
          [http://en.wikipedia.org/wiki/Information_hiding]

          ~
          • 2. Re: Why do we declare the variables private in a bean
            800304
            Hi^^,
            thank you for the article you have supplied. Two lines used in the article are interesting.They are as follows:
            1.Information hiding serves as an effective criterion for dividing any piece of equipment, software or hardware, into modules of functionality.

            2.When a computer program is well designed decomposing the source code solution into modules using the principle of information hiding, evolutionary changes are much easier because the changes typically are local rather than global changes.

            So in the bean concept how are we using the principle of information hiding. Are we diving the MVC process into modules or what. Please pardon me as I am not able to link this up.

            A bean is actually a simple java class composed of the follwing
            1. Private variables.
            2. Zero argument constructor
            3. Getters and setters.
            I repeat my question, why do we declare the varibles private
            • 3. Re: Why do we declare the variables private in a bean
              807601
              So in the bean concept how are we using the principle of information hiding. Are we diving the MVC process into modules or what.
              Not necessarily modules in the case of MVC, but rather different layers of functionality. I think of modules as being more vertical in nature, whereas MVC is horizontal. But that's largely semantic; the idea is that data is kept private to insulate other layers from coupling and changes.
              I repeat my question, why do we declare the varibles private
              The answer is contained (at least in part) in the two lines you mentioned. I'm not sure I can describe it in a better way. What do you find confusing about the answer? If it's just a matter of the concept not yet completely sinking in, practice and experience can help with that.

              ~
              • 4. Re: Why do we declare the variables private in a bean
                800304
                Hi ^^,
                thanks for replying.I had a discussion with one of my trainers and he was of the opinion that the variables should generally be declared private. In this way we prevent classes from accessing and setting illegal values to those variables.

                Say for instance we have the class person as follows:
                public class Person
                {
                int age;
                setAge(int age)
                {
                if (age < 0)
                return null
                }
                Person p = new Person() ;
                p.age = -2;
                //this would be perfectly legal
                ___________________________________________________________________________
                //where as if we declare the variable as private as follows:
                public class Person
                {
                private int age;
                setAge(int age)
                {
                if (age < 0)
                return null
                }
                Person p = new Person() ;
                // P.age = -2;   this would be illegal as age is private and would have to be accessed by the method defined above.
                p.setAge(-2);
                //the cbove line would retun null values.
                • 5. Re: Why do we declare the variables private in a bean
                  807601
                  pksingh79 wrote:
                  Hi ^^,
                  thanks for replying.I had a discussion with one of my trainers and he was of the opinion that the variables should generally be declared private. In this way we prevent classes from accessing and setting illegal values to those variables.

                  Say for instance we have the class person as follows:
                  public class Person
                  {
                  int age;
                  setAge(int age)
                  {
                  if (age < 0)
                  return null
                  }
                  Person p = new Person() ;
                  p.age = -2;
                  //this would be perfectly legal
                  ___________________________________________________________________________
                  //where as if we declare the variable as private as follows:
                  public class Person
                  {
                  private int age;
                  setAge(int age)
                  {
                  if (age < 0)
                  return null
                  }
                  Person p = new Person() ;
                  // P.age = -2;   this would be illegal as age is private and would have to be accessed by the method defined above.
                  p.setAge(-2);
                  //the cbove line would retun null values.
                  public class Person {
                      private int age;
                  
                      public void setAge(int age) {
                          if (age < 0) {
                              throw new IllegalArgumentException("...");
                          }
                          this.age = age;
                      }
                  }
                  • 6. Re: Why do we declare the variables private in a bean
                    807601
                    In this way we prevent classes from accessing and setting illegal values to those variables.
                    Yes, that's one benefit.

                    ~