Hi all,
I have a question regarding static methods, I have done some reading up on them but cant quite get my head round them with regard to my situation. I have recently undertaken a huge re-write of our product replacing a mix of old primitive long & object Long id's with a new IDObject.
Im many areas these id's have to be reconstituted from a String value (i.e. for passing as a URL parameter). Im a big fan of neat code, so rather than having
IDObject id = null;
String s = getStringParam();
if( s != null ){
id = new IDObject( s );
}
I just wanted to be able to do :-
IDObject id = new IDObject( getStringParam() );
That was fine until NULLS appeared on the scene, and I dont want to create an object holding a "null" value; I really want to be able to have
if( IDObject == null ) ......
rather than something like
if( IDObject.isNull() )
as theres waaay too much code to check and rewrite to do the latter ( I can smell the null pointer exceptions already ! )
so (getting to the point at last) .. to get round this I wrote a set of static methods in the ID class to check the input value and return either null, or call the relevant constructor and return that.
e.g.
public static IDObjectgenerateBisID( String stringID ){
if( stringID == null ){
return null;
}
return new IDObject( stringID );
}
Im worried about the safety of this as its in a multithreaded environment, but not really sure how a static behaves in this case.... should I be? Im thinking it is safe as it doesnt make use of anything other than the constructor - but theres always that nagging doubt
I thought about making them synchronized for safety, but this code is hit a LOT and am worried that would become a bottleneck (havent done any testing in this regard yet tho).
Thanks for reading this far
<< JOC >>
I have a question regarding static methods, I have done some reading up on them but cant quite get my head round them with regard to my situation. I have recently undertaken a huge re-write of our product replacing a mix of old primitive long & object Long id's with a new IDObject.
Im many areas these id's have to be reconstituted from a String value (i.e. for passing as a URL parameter). Im a big fan of neat code, so rather than having
IDObject id = null;
String s = getStringParam();
if( s != null ){
id = new IDObject( s );
}
I just wanted to be able to do :-
IDObject id = new IDObject( getStringParam() );
That was fine until NULLS appeared on the scene, and I dont want to create an object holding a "null" value; I really want to be able to have
if( IDObject == null ) ......
rather than something like
if( IDObject.isNull() )
as theres waaay too much code to check and rewrite to do the latter ( I can smell the null pointer exceptions already ! )
so (getting to the point at last) .. to get round this I wrote a set of static methods in the ID class to check the input value and return either null, or call the relevant constructor and return that.
e.g.
public static IDObjectgenerateBisID( String stringID ){
if( stringID == null ){
return null;
}
return new IDObject( stringID );
}
Im worried about the safety of this as its in a multithreaded environment, but not really sure how a static behaves in this case.... should I be? Im thinking it is safe as it doesnt make use of anything other than the constructor - but theres always that nagging doubt
I thought about making them synchronized for safety, but this code is hit a LOT and am worried that would become a bottleneck (havent done any testing in this regard yet tho).
Thanks for reading this far
<< JOC >>