Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By doWhile

Thread: Is it bad practice to make attributes public so other classes can call their methods?

  1. #1
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default Is it bad practice to make attributes public so other classes can call their methods?

    This is for a game I need to code, an Oregon Trail style game, but I've always been curious about this.

    For example, my main method has the Wagon object, and the wagon is holding the different people. If I want to modify a Persons health with setHealth(int h), could I just make the people a public attribute (or array) and the main method could directly modify them?

    This seems a lot easier, and I won't have to put a new setter in Wagon just to modify its contents.

    thanks for the help guys

  2. #2
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Is it bad practice to make attributes public so other classes can call their meth

    Good question! I am wondering the same thing.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Is it bad practice to make attributes public so other classes can call their meth

    It's usually a good practice to use getters and setters. This allows you to fully control visibility of your properties and allows you to do validity checking prior to setting a property. It also gives other flexibility such as allowing you to later make your property a "bound" property by using PropertyChangeSupport so that other classes (or this class) can listen and react to changes in this property -- something that I've found to be very useful.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Is it bad practice to make attributes public so other classes can call their meth

    Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    To quote the advice from the article above:
    Use the most restrictive access level that makes sense for a particular member. Use private unless you have a good reason not to.
    In other words - it is good practice to encapsulate variables - making variables public can cause all sorts of problems when it comes to a good deal of things, especially code maintenance.
    Fubarable likes this.

  5. #5
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Is it bad practice to make attributes public so other classes can call their meth

    Thank you all

Similar Threads

  1. Call Static Methods Outside Classes
    By Insomniac Riot in forum New To Java
    Replies: 4
    Last Post: 05-11-2010, 11:03 PM
  2. Methods and Attributes Layout
    By Bascotie in forum New To Java
    Replies: 2
    Last Post: 01-16-2009, 01:53 AM
  3. Classes with object attributes
    By RRasco in forum New To Java
    Replies: 5
    Last Post: 11-25-2008, 01:07 AM
  4. [SOLVED] Best practice for creating test classes
    By mwildam in forum New To Java
    Replies: 3
    Last Post: 08-27-2008, 05:59 PM
  5. How to call methods of different classes
    By adeeb in forum New To Java
    Replies: 2
    Last Post: 06-06-2008, 07:08 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
  •