Results 1 to 7 of 7
Like Tree2Likes
  • 2 Post By JosAH

Thread: Why private variables

  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Why private variables

    I have watched some programming tutorials and the guy in the tutorial said that you shouldn't use public variables and you should always use private variables.
    Why should you use private variables instead of public variables?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default Re: Why private variables

    Surely there are answers to this question on google, or even on this forum? What happened when you did a search? Why do you think you should use private variables?

    The gist is this: public variables defeat several principles of object-oriented design, including encapsulation and detail-hiding, plus it opens your code up to malicious (or stupid) code making unwanted changes to your variables.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why private variables

    Nobody wants his/her private parts touched or modified without their explicit permission.

    kind regards,

    Jos
    Tolls and sunde887 like this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Why private variables

    public variables can be altered by other classes in your program. Keeping your variables private shuts up shop basically but you still need to watch for privacy leaks in your code.

    Is this right by the way lol? I'm still learning this stuff....

    Best Regards

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: Why private variables

    Quote Originally Posted by lannie1980 View Post
    public variables can be altered by other classes in your program. Keeping your variables private shuts up shop basically but you still need to watch for privacy leaks in your code.

    Is this right by the way lol? I'm still learning this stuff....

    Best Regards
    That's a pretty good description.
    As Jos says, you don't want someone fiddling with your attributes without you knowing about it.

    Your point about privacy leaks is also good. Date is a common example on this:
    Java Code:
    public class ExposedDate {
       private Date myDate;
    
       public Date getMyDate() {
          return myDate;
       }
       public void setMyDate(Date myDate) {
          //Do some validation stuff here
          if (validateMyDate(myDate)) {
             this.myDate = myDate;
          }
       }
    }
    'myDate' is nicely encapsulated there. Whenever it is changed we validate it (for whatever reason), possibly changing some other data in this object to reflect the Date (say for weekends).
    However, our getter hands over a reference to that Date object. Since java.util.Date is not immutable (ie it has setters and can therefore be changed) someone can now get myDate and change it without it being validated.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why private variables

    Quote Originally Posted by Tolls View Post
    However, our getter hands over a reference to that Date object. Since java.util.Date is not immutable (ie it has setters and can therefore be changed) someone can now get myDate and change it without it being validated.
    That is a very nasty issue indeed; read "Demeter's law"; it tries to deal with this issue (by contract).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 06-03-2011, 10:15 PM
  2. Why make class variables private?
    By PrinceSendai in forum New To Java
    Replies: 3
    Last Post: 10-18-2010, 12:01 PM
  3. Inheritance - accessing private variables
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 04-26-2010, 02:21 AM
  4. OOP Question re. private variables and extending classes
    By ImplicitCharm in forum New To Java
    Replies: 7
    Last Post: 07-28-2009, 04:46 PM
  5. Replies: 3
    Last Post: 03-14-2009, 08:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •