954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Where to declare varaiables?

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.

rdhiravani
Light Poster
37 posts since Jan 2007
Reputation Points: 10
Solved Threads: 2
 

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

Dukane
Posting Whiz in Training
295 posts since Oct 2006
Reputation Points: 45
Solved Threads: 29
 

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
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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?

hussulinux
Light Poster
42 posts since Feb 2007
Reputation Points: 10
Solved Threads: 2
 

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
Team Colleague
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
Administrator
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
Moderator
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
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 
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.

rdhiravani
Light Poster
37 posts since Jan 2007
Reputation Points: 10
Solved Threads: 2
 

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.

rdhiravani
Light Poster
37 posts since Jan 2007
Reputation Points: 10
Solved Threads: 2
 

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
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You