Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

question regarding class level vs local variables  RSS feed

 
Mike Ottinger
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've been told to create local variables rather than class variables for methods because of the compiler's ability to reference them easily from local where as class level the compiler takes a more indirect way to gain access to them. This makes sense, but what about in a scenario where a particular method is called repeatedly, and in that method it creates a somewhat large variable each time. I had moved the variable to class level and basically reuse that variable rather than having it created each time. This approach basically breaks the first rule above. Any comments on which tack to follow?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I doubt you could measure the difference in access time. It would be interesting to look at the actual compiled byte code to see what is involved.
Much more important would be:
1. Synchronization issues - can this class variable be changed once created? Will multiple Threads be accessing it? Should it be an instance variable?
2. Instance construction time - if your application makes a lot of instances, the extra time to create this variable could mount up.

Generally speaking, go for clarity instead of some purported speed advantage.
Bill
 
Free Bird (Kynard)
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I feel that the scenario is totally need based.

The local variables in a function or a block have a stack implementation.
The class variables have a different memory location copy for each object.

The analysis would be that is it faster for the JVM to just pop in and pop out or reference the object and retrieve the variable from the memory. I think if you are talking about memory then there needs to be a negligible difference...in fact the difference should be totally unperceivable !!!

but I think the local variables should do what they are supposed to and class variables the same�.

There needs to be no contention between them
 
Mike Ottinger
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taking both approaches I didn't see any sizable difference. I'm not interested in performance metrics that are so infinitesimally small with the difference between class vs local access. Thanks for the info guys.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not interested in performance metrics that are so infinitesimally small with the difference between class vs local access.

That is music to my ears!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!