How to give a local variable higher local scope

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

5 comments
Comment actions Permalink

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

0
Comment actions Permalink

Yes. If you try to use the variable above and after it's declared
scope, you will be offered and intention 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?




0
Comment actions Permalink

Dave, many 10x for the help!!

Chris Miller wrote:


Nice How long has this feature existed? Not sure how I managed to miss
that one.


Chirs, I also wonder how could I miss it.... :)

It's a little buggy though, I just tried with the following

(slightly dubious) example:

for (int i = 0; i < 10; i++) {
System.out.println(i);
}
i = 0;

This resulted in:

int i;
for (; i = 0;i < 10; i++) {
System.out.println(i);
}
i = 0;

...which doesn't compile.


Definitely a bug, but IDEA guys are good and will fix it ;)


Another small bug is that it doesn't initialise the variable declaration
when it is needed. Eg:

try {
Connection con = <get database connection>;
...
}
finally {
DatabaseUtil.close(con); // This method doesn't mind con == null.
}

...results in:

Connection con;
try {
con = <get database connection>;
...
}
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).


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

0
Comment actions Permalink

Shall I create Jira issues for these?

Yes, particularly the first one. (Check to see whether any exist first, of course.)

--Dave Griffith

0
Comment actions Permalink

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.


0

Please sign in to leave a comment.