java


private static method in class pros and cons?


Sometimes when writing a class you'll need some helper methods that take care of some simple stuff (change a string in a certain way or do some simple calculations).
If helper functionalities are small enough (and not needed by any other class) it makes sense to write a helper method in this class.
Now: assuming you'll need no access to any member variables, would it be better do make this method private or private static
the following example: just implements a method that checks if a string is not null and contains foo.
public class SomeClass
...
public void calculate(String inputString) {
...
boolean foo = getFoo(inputString);
}
private (static) boolean getFoo(String inputString) {
return inputString != null && inputString.contains("foo");
}
}
Are there pros and cons in making the method static or non-static. Is there a general do and don't?
I would personally make the method static - it makes it clearer that it doesn't depend on the state of the object. That can have knock-on effects in terms of thread safety (although if it were to access static members of the type, you'd need to be careful of that).
Given that it's private, you don't need to worry about whether or not subclasses would want to override it.
You can always make it non-static later - I think the only side-effect of that would be to potentially change the serialization ID. (Binary serialization is a pain like that.)
Make it non-static if it needs access to non-static member variables, and make it static if it doesn't.

Related Links

go beyond Integer.MAX_VALUE constraints in Java
appengine - java persistent
Designing an XACML API
Building a custom BSD version that supports only Java
How to generate a signed PDF w/o green checkmark
Java GUI Creating Components
Generated equals in Webservice Stub
Java regexp for file filtering
How to download page for jad file at servlet?
What is the best way to connect to a DotNet web service from java?
Need a basic audio & video player library in Java [closed]
Why doesn't Java support unsigned ints?
JInternalFrame Does Not Fully Maximize Under Mac OS X
Eclipse debug-time classpath problem: How do you include a dependent project's output into a web project's runtime classpath?
How to set a timeout during remote ejb lookup?
Best Java supported server/client protocol?

Categories

HOME
log4j
multithreading
keras
hashmap
kalman-filter
jira
setup-deployment
datastax-java-driver
django-imagekit
google-translate
node-pdfkit
google-apps-marketplace
windows-phone-7
designer
plunker
visjs
java-3d
functional-testing
dcevm
telerik-reporting
cultureinfo
buildbot
pepper
su
bootstrap-duallistbox
traffic
tooltipster
social-media
objectanimator
srcset
google-rich-snippets
oracle-fusion-middleware
calibre
grails-3.1
usbserial
http-digest
overlap
vxworks
az-application-insights
unobtrusive-validation
squib
eigenvalue
password-encryption
wptoolkit
scrollable
hibernate-tools
typescript1.8
domain-model
dynamics-sl
wdf
mikroc
thrust
etsy
blackberry-10
revolution-r
two-factor-authentication
separator
wso2cloud
sankey-diagram
generic-programming
mmc
composite
remobjects
oberon
device-orientation
umbraco6
system.reflection
modalpopup
typekit
file-copying
issuu
android-radiobutton
rtmfp
algebraic-data-types
page-layout
ivyde
xsockets.net
jsctypes
jquery-mobile-dialog
free-variable
mt
word-processor
datawindow
hosts-file
getmessage
parametric-equations
online-compilation
dotnethighcharts
bigcouch
window-management
mod-auth
django-tagging
appender
gamequery
meego
temporal-database
scripting-languages
ugc

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App