How to give a local variable higher local scope Follow
Hello all,
Is there some Intelli-gent way to move a local variable to an upper
local scope? E.g.
if(someCondition())
{
LocalVar var = new LocalVar();
// do s/th with var, inside the if-scope only
}
And now I am searching for refactoring or something that would
produce the following:
LocalVar var; // <- this is now available outside the if-scope. if(someCondition()) { var = new LocalVar(); // do s/th with var }]]>
Namely, the LocalVar variable is now declared in the scope that
bounds the scope of the "if".
If there isn't something like this, will be a JIRA issue appreciated?
Greetings to all!
George
Please sign in to leave a comment.
Yes. If you try to use the variable above and after it's declared scope, you will be offered andintention to move the variable declaration into scope. Thus, you can do exactly what you want, but only if it's actually necessary. Yes, this if very cool.
--Dave Griffith
Nice! How long has this feature existed? Not sure how I managed to miss that
one. It's a little buggy though, I just tried with the following (slightly
dubious) example:
for (int i = 0; i ; ... } finally { DatabaseUtil.close(con); // This method doesn't mind con == null. } ...results in: Connection con; try { con = ]]>;
...
}
finally {
DatabaseUtil.close(con); // This method doesn't mind con == null.
}
which again, doesn't compile (although the "initialize variable 'con'" intention
makes it easy to correct).
Shall I create Jira issues for these?
Dave, many 10x for the help!!
Chris Miller wrote:
Chirs, I also wonder how could I miss it.... :)
It's a little buggy though, I just tried with the following
Definitely a bug, but IDEA guys are good and will fix it ;)
I tend to forgive this sin to IDEA since "initialize variable" does the
job, although in two steps. But if IDEA guys are willing to enhance the
functionality, I will not complain :)
Best greetings to all!
George
Shall I create Jira issues for these?
Yes, particularly the first one. (Check to see whether any exist first, of course.)
--Dave Griffith
OK, I couldn't find anything similar so I've created the following two issues:
http://www.jetbrains.net/jira/browse/IDEA-16518
http://www.jetbrains.net/jira/browse/IDEA-16520
Cheers.