0

Assume a scenario where we can use a local variable vs access an instance variable what should I chose ?

eg: inside foo.java

int x = bar.getX();
int y = x * 5;

Advantage is that we dont have performance impact of accessing object variable over and over.

or inside foo.java

int y = bar.getX() * 5;

Advantage of using the second method, is we dont need to create and maintain another variable.

5
  • premature optimization is teh devil. In the real world this makes absolutely no difference. And, you're really not as good as the JIT. Commented Dec 21, 2013 at 6:52
  • I dont understand what's the optimization done here ? Whats your stance either create a new local variable or not to ? Commented Dec 21, 2013 at 6:53
  • "Which should I choose?" ... well, what would be the driving force behind being concerned over the choice? The result is the same. The only reason for concern is premature optimization unless you're profiles code and determined one way is significantly faster than another, which in this case, it wouldn't be. Commented Dec 21, 2013 at 6:56
  • Apologize but where is the optimization in this 2 codes ? Its just a choice between the two. what would be the driving force behind being concerned over the choice? - thats exactly what i intend to get answered Commented Dec 21, 2013 at 6:59
  • You have two versions of something. The outcome is the same. Why do you care which is "best" (e.g. which should I do) if you're not trying to optimize? If it's a question of style, that's a completely different question and is ultimately ambiguous as it's personal preference / readability which the A you accepted covers. Commented Dec 21, 2013 at 7:03

2 Answers 2

3

Do whatever is more readable.

Usually the shorter code is easier to read, comprehend, and show correct, but if an expression becomes long and the itermediate expressions make sense on their own, then introduce a local variable to make the code clearer.

As far as performance goes, you don't know that there will be any performance hit one way or the other unless you profile the code. Optimization in Java happens at compile but also at runtime with modern JIT compliation. Compilers can do inlining, common subexpression elimination and all kinds of algebraic transformations and strength reductions that I doubt that your example will show any performance impact one way or the other.

And unless an expression like this is executed a billion times or more in a loop, readability wins.

Sign up to request clarification or add additional context in comments.

Comments

2

Thumb rule is reduce the scope of variable as much as you can.

If you want access of variable across all the methods, use instance member. Otherwise simply go for local variable.

If you are using your variable X across methods, define it as a member.

Coming to the performance impact , int x = obj.getX(); simply you are creating a copy variable and nothing else.

You'll realize once you use the Object and not the primitive. Then reference matters.

So in your case the first line int x = obj.getX(); is little redundant. Second is more readable.

2 Comments

seems like there is somemisunderstanding. I am not asking when to use local vs when to use instance var. I am saying I have an object "bar" with instance var x, what should I do in object foo ? do bar.getX() all the time Or do int x = bar.getX() and use local x in foo ?
@JavaDeveloper No need to worry. I wont prefer to create that variable with returned value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.