Results 1 to 4 of 4
  1. #1
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Lima, Peru
    Rep Power

    Default 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.

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Destiny Islands
    Rep Power


    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:
    Java Code:
    public void AppendElement(E elem) {
         this.array.Push(elem); # Fictional function
         global.user.Update(this); # Fictional function
    ...whereas, if this was a public variable, another coder could just do "someObj.array.Push(variable);" and the Update method would never be called.

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    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.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power


    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 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

  1. Inheritance - accessing private variables
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 04-26-2010, 02:21 AM
  2. Public, private or (nothing) class
    By tyang in forum New To Java
    Replies: 3
    Last Post: 02-01-2010, 12:37 AM
  3. Replies: 1
    Last Post: 01-03-2010, 10:48 PM
  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