Results 1 to 4 of 4
- 02-07-2011, 06:21 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
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?
- 02-07-2011, 06:34 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-07-2011, 07:01 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 30
- Rep Power
- 0
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 09:07 PM.
- 02-07-2011, 08:54 PM #4
Similar Threads
-
Netbeans versus JavaEditor: main class?
By ruud00000 in forum New To JavaReplies: 3Last Post: 01-10-2011, 08:52 AM -
public variables?
By dyelax in forum New To JavaReplies: 9Last Post: 12-27-2010, 11:17 PM -
Access Public Global Variables in class
By spatel14 in forum New To JavaReplies: 5Last Post: 07-08-2010, 10:50 AM -
Access Public Global Variables in class
By spatel14 in forum New To JavaReplies: 1Last Post: 07-07-2010, 07:41 PM -
Accessing non-static public variables from another class
By ribbs2521 in forum New To JavaReplies: 4Last Post: 10-22-2009, 05:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks