Results 1 to 4 of 4
  1. #1
    jezza10181 is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default public class variables versus methods

    Hi,

    I have written a class that takes the 'nth root' of a java BigDecimal, but I am wondering how to structure it.

    I need to provide access to a number of the class variables. Obviously, the root itself, and also the 'number of iterations' performed by the root finding algorithm, are 'returned'.

    The way i've usually done this, is by declaring the class variables in question as public variables. However, I have come across an algorithm that performs square roots, and it returns a number of variables, but it uses methods to do so.

    What is the better way of doing this?? Is the method route preferable over declaring class variables as public?

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

    Default

    Quote Originally Posted by jezza10181 View Post
    Hi,

    I have written a class that takes the 'nth root' of a java BigDecimal, but I am wondering how to structure it.

    I need to provide access to a number of the class variables. Obviously, the root itself, and also the 'number of iterations' performed by the root finding algorithm, are 'returned'.

    The way i've usually done this, is by declaring the class variables in question as public variables. However, I have come across an algorithm that performs square roots, and it returns a number of variables, but it uses methods to do so.

    What is the better way of doing this?? Is the method route preferable over declaring class variables as public?
    When you write software only used by you, it doesn't matter much (it still matters though) but think what could happen if your software is going to be used by others: those public variables are defenseless, they are just there stark naked: they can be changed by anyone who doesn't know what s/he is supposed to do and ruins all your clever algorithms etc. Getters and setters are preferable in such situations. Note that that ignorant person might be you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    codesmuggler is offline Member
    Join Date
    Feb 2011
    Posts
    30
    Rep Power
    0

    Default

    jezza10181:
    It's good to program in a way that your code could be reused by someone else. If you are learning Java programming you might not see that keeping fields private and generating setters and getters for them is a proper way (too much code etc.), but you will see advantage in the future.
    Last edited by codesmuggler; 02-12-2011 at 10:07 PM.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    generating setters and getters for them is a proper way
    Don't forget IDEs make this simple to do! Both netbeans and eclipse can 'encapsulate' fields which allows you to generate getters/setters for everything with 1 click!

Similar Threads

  1. Netbeans versus JavaEditor: main class?
    By ruud00000 in forum New To Java
    Replies: 3
    Last Post: 01-10-2011, 09:52 AM
  2. public variables?
    By dyelax in forum New To Java
    Replies: 9
    Last Post: 12-28-2010, 12:17 AM
  3. Access Public Global Variables in class
    By spatel14 in forum New To Java
    Replies: 5
    Last Post: 07-08-2010, 11:50 AM
  4. Access Public Global Variables in class
    By spatel14 in forum New To Java
    Replies: 1
    Last Post: 07-07-2010, 08:41 PM
  5. Replies: 4
    Last Post: 10-22-2009, 06:45 PM

Posting Permissions

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