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

How to implement autocompletetextview in Android Studio with an API call?
How to solve race condition of two writers using immutable objects
Stream audio as if from microphone on Android [duplicate]
RuntimeException - java.lang.IllegalArgumentException: column '_id' does not exist [duplicate]
A 'if else' code block returns the 'else' sentence even doesn't need
“Is not mapped” expection with Hibernate
Should I represent a Java utilitary class in UML?
How to get location of all the app user and show it on map
Java JFrame is not visible when i later call a new class
How to put method calls in a stack
Remove xml nodes not in XSD schema
Google App Engine FreeMarker template java.security.AccessControlException
Proguard error log giving wrong library/class name
why arraylist clear to temporery as well as original?
Searching Binary Tree with a While loop
How to exclude WEB-INF/libs from being deployed in wildfly

Categories

HOME
cakephp
bluetooth
wso2
json.net
electron
q
routes
elm
graphql
binary-tree
fingerprint
android-youtube-api
facebook-messenger-bot
amazon-cloudformation
here-api
rascal
phaser
clojurescript
after-effects
ef-migrations
jsdoc
angular-ui
aurigma
solaris-10
reactive-cocoa
ab-testing
kudan
su
entitlements
webtest
commit
language-concepts
revolution-slider
vxworks
xcode-extension
hot-module-replacement
mixture-model
hilbert-curve
uft-api
jna
lumberjack
greenrobot-eventbus
dynamic-reports
executenonquery
scrollable
parentheses
boost-multi-index
blogengine.net
togetherjs
gabor-filter
mplayer
elgg
flutterwave
spring-cache
sonarlint-vs
pintos
jquery-filter
setuptools
gridpane
log4c
mikroc
etsy
django-debug-toolbar
gridview-sorting
angstrom-linux
flash-cs5
simplewebrtc
ready-api
essence
make-install
xpath-1.0
pyke
ios8-today-widget
metaclass
computer-algebra-systems
splash
appfabric-cache
apache-commons-fileupload
lustre
nsmutabledictionary
modalpopup
typekit
ng-animate
nsbutton
preferences
zend-route
mesa
jquery-layout
expected-exception
htmlcleaner
mysql-error-1062
poller
kgdb
message-driven-bean
gnu-smalltalk
html4
android-screen-support
type-equivalence
kyotocabinet
quickdialog
objective-c-2.0
wse3.0
adk
libc++
hirefire
hamachi
appender
appendto
locate
asp.net-mvc-areas
digest-authentication
gin
vc90
ncqrs
substrings
aquaticprime
mdac
wsdl.exe

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App