Results 1 to 4 of 4
- 10-17-2010, 04:24 AM #1
Member
- Join Date
- Jul 2010
- Location
- Lima, Peru
- Posts
- 47
- Rep Power
- 0
Why make class variables private?
Okay, so I keep coming across the practice of making class variable private and using getter and setter methods for other classes to interact with these variables.
I tried looking for some explanations but what I have found is that it is "good practice" to do so.
However, as I code, sometimes I find my code is longer and it is more tedious to write when I need to write getter and setter methods for everything. Why not just make them public? Especially when it is not a big deal. So basically, I'm looking for real reasons for not making them public, apart from being "good practice".
I mean, I understand if you don't want an external class to modify these methods by accident, yet this external class could access the getter and setter methods, producing the exact same result.
Sorry for my ignorance in this respect.
- 10-17-2010, 04:33 AM #2
The reason I've always been given and that I continue to perpetuate doesn't apply when you are working alone on a project. When you're working with others, however, and you have some class wherein the variables should not be modified by other classes, then it tells your "partner" that those variables should not be modified by him.
There is one other reason I can think of. Think of a class that holds an array, and every time that array is updated, it has to send that update to the client. So it has stuff like:
...whereas, if this was a public variable, another coder could just do "someObj.array.Push(variable);" and the Update method would never be called.Java Code:public void AppendElement(E elem) { this.array.Push(elem); # Fictional function global.user.Update(this); # Fictional function }
Again, these only apply when working with others--hence why they are just labelled as "good practice".
Perhaps others have more ideas as to why, but that's my $0.02.
-
The larger your projects become, the greater the number of interactions that can occur between different objects. You want to control and limit these interactions to the bare minimum else difficult to discover side effects can and will creep into your code. Encapsulation (detail hiding) is one technique to help you do this.
- 10-18-2010, 11:01 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
What they said...and it even applies if you're a one-man-band. It's easy to lose track of where things are changed if you don't encapsulate your data.
If it's encapsulated (get/set) then you can easily debug to find out when and where an attribute is changed...place a breakpoint in the setter and away you go. If you had public attributes then tracking who and where that attribute was changed becomes...well...entertaining.
Similar Threads
-
Inheritance - accessing private variables
By counterfox in forum New To JavaReplies: 3Last Post: 04-26-2010, 01:21 AM -
Public, private or (nothing) class
By tyang in forum New To JavaReplies: 3Last Post: 01-31-2010, 11:37 PM -
Exception Class for class that compares objects variables. Help Please.
By darkblue24 in forum New To JavaReplies: 1Last Post: 01-03-2010, 09:48 PM -
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


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks