If they are constants, you can declare them "static final" and they will only be allocated once and directly inlined into expressions.
Also, if you need high performance concatenation of many strings ( as in your "String str = v1 + v2 + v3;" statement), use a StringBuffer instead.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
no, that wouldn't matter one bit (unless MAYBE they're all primitives).
And when the time almost inevitably comes that that class is used in a multithreaded environment it's far easier to get it threadsafe if there are no or very few instance variables (and especially statics).
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
> Also, if you need high performance concatenation of many strings use a StringBuffer instead.
StringBuffer is thread safe. If programming in a single threaded environment, I would recommend StringBuilder.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Well, from the code he posted, they are all primitive constants, so declaring them as final statics on the class should alleviate all of his concerns about the overhead. He says that his method just appends them together based upon some conditions, so it sounds like using a StringBuffer to build his result from the constants would offer him the best performance.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
> Also, if you need high performance concatenation of many strings use a StringBuffer instead.
StringBuffer is thread safe. If programming in a single threaded environment, I would recommend StringBuilder.
Good point. I tend to forget about that one since we were stuck at 1.4 code base for a long time hehe
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
no, instance variables are (as I pointed out) NOT a good idea in a multithreaded environment.
Pretty much all coding standards btw tell you to limit the scope of variables to the smallest possible. So do not use instance variables unless you need to share information between methods (aka maintain state between method invocations).
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337