| Author |
private static method in a class
|
Ingudam Manoranjan
Ranch Hand
Joined: Jul 31, 2006
Posts: 48
|
|
Hi, Can anyone tell me under what circumstance can we use private static method in a class and Why? Thanks.
|
 |
Matt Harrah
Ranch Hand
Joined: Aug 05, 2006
Posts: 54
|
|
|
One might be to serve as a helper method to a public static method, but the helper method should not be visible outside the class.
|
 |
John Kelty
Ranch Hand
Joined: Aug 08, 2006
Posts: 33
|
|
Exactly. As a concrete example, consider Java�s Integer class, which has a series of static methods that take an int parameter and return a String version of the int. There�s toHexString(int), toOctalString(int), and toBinaryString(int). The functionality behind these is all very similar, so naturally Java uses a static helper method, static String toUnsignedString(int num, int exp). The method actually has package visibility (because the functionality is also used by class Long) so it�s not quite private, but the basic reasoning behind limiting access to a static helper method is the same.
|
 |
Arvind Sampath
Ranch Hand
Joined: May 11, 2005
Posts: 144
|
|
Wow! Are you, by any chance, part of the SUN API Development team, John ?
|
 |
Ingudam Manoranjan
Ranch Hand
Joined: Jul 31, 2006
Posts: 48
|
|
Thanks Matt. Thanks John. I am clearer now .
|
 |
John Kelty
Ranch Hand
Joined: Aug 08, 2006
Posts: 33
|
|
No, nothing nearly that glamerous! I recently did some work with conversios between ints and Strings, so I knew there was something like that in Integer, and then I just googled for the source.
|
 |
 |
|
|
subject: private static method in a class
|
|
|