Results 1 to 7 of 7
Thread: Why private variables
- 06-06-2012, 07:04 PM #1
- 06-06-2012, 07:08 PM #2
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!
- 06-06-2012, 08:03 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,411
- Blog Entries
- 7
- Rep Power
- 17
Re: Why private variables
Nobody wants his/her private parts touched or modified without their explicit permission.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-06-2012, 11:47 PM #4
Member
- Join Date
- Mar 2012
- Posts
- 88
- Rep Power
- 0
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
- 06-07-2012, 09:55 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Why private variables
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:
'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).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; } } }
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.
- 06-07-2012, 11:38 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,411
- Blog Entries
- 7
- Rep Power
- 17
- 06-07-2012, 09:11 PM #7
Member
- Join Date
- Mar 2012
- Posts
- 88
- Rep Power
- 0
Similar Threads
-
Can you use the super keyword when variables in parent class are private?
By lam5442 in forum New To JavaReplies: 2Last Post: 06-03-2011, 09:15 PM -
Why make class variables private?
By PrinceSendai in forum New To JavaReplies: 3Last Post: 10-18-2010, 11:01 AM -
Inheritance - accessing private variables
By counterfox in forum New To JavaReplies: 3Last Post: 04-26-2010, 01:21 AM -
OOP Question re. private variables and extending classes
By ImplicitCharm in forum New To JavaReplies: 7Last Post: 07-28-2009, 03:46 PM -
Private or Protected access for super class variables
By Madushan in forum New To JavaReplies: 3Last Post: 03-14-2009, 07:22 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks