Hi
I have a method, which is called more than 50 times a second.
I have multiple(more than 10) individual variables of different data types initialised in that method & in last part of the method, a single String is returned appending the value of all variables (with conditions on value).

So it is good idea to have these individual variables as local to that method, for which each call statement to that method will cause allocation of memory for them; or should I declare it as member variable of class.
The vaiables are not useful outside method.
eg

String methodName() {
int v1 = 98;
float v2 = 1.0;
long v3 = 2947239L;
.
.
char v10 ='R';

String str = v1 + v2 + v3;
return str;
}

Thanks in advance.

Recommended Answers

All 10 Replies

Member Avatar for Dukane

Then you should keep them local to the method. Let Java worry about handling the memory.

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.

I would put them as instance variables rather than local because the method is being called 50 times a second... Imaging your JVM creating and destroying the variables so many times...!! its a huge pressure on it... So why not just let it breathe and keep it as instance variable and as private... What do you think?

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).

> 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.

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.

> 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

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.

Thanks for reply.
Yeah, StringBuffer is good idea (It was suggested by other ppl also).
In my example code I have initialised it as a constant, but they r not constants.

Actually declaring all vaiables as instance variables is good idea from memory point of view, but instance variable mainly are such variables which are accessed( &/or processed) by more than 1 methods. That why I am confused.
I want to know, what Java Coding Standard suggests.

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.