Question

Out of curiosity I tried this out to see if it was valid code inside a method:

{
    // code
}

And it is! All it does is limit the scope:

{
    int i = 1;
}

i = 2; // error

This is nice for e.g. when I've needed to do something that looks structurally like this:

int i = 0;
for (; i < number; i++) {...}

doSomethingWithTheCount(i);

But I'd like to limit the scope of i:

{
    int i = 0;
    for (; i < number; i++) {...}

    doSomethingWithTheCount(i);
}

And indeed I can. Is this useful for anything else or just obscure?

Additionally, I've read that limiting the scope of an object is a good way to ensure it is garbage collected sooner so potentially doing this is beneficial in that regard. Is that correct?

Was it helpful?

Solution

There is nothing inherently wrong in declaring anonymous blocks, so you don't have to worry about it as such. Some people may have opinions about it following Java coding conventions or other ideas of a more or less aesthetic nature, but that's subjective and up to you to decide the validity of. I, on my hand, would have no such issues with it.

However, it is not correct that it helps garbage collection. In the compiled bytecode, the local variables persist beyond the end of the block's scope and will retain their values unless you explicitly clear them to null; the block only affects symbol allocation in the Java source, and does not translate to anything that the JVM sees.

Additionally, it may be worth noting that you can also use these kinds of blocks for control-flow purposes, like this:

foo: {
    /* ... */
    if(bar)
        break foo;
    /* ... */
}

Unfortunately, Java does not support continue foo for custom looping logic. :(

OTHER TIPS

You can limit scope like this if you really feel the need, but the compiler trivially detects that your variable will not be used again before the end of the method and will reuse its memory or register location if it's convenient.

Regarding your edit about GC, (1) ints aren't GCd, (2) I would bet that once the compiler detects the end of a variable's use within its scope, it marks the scope as over for purposes of GC, and (3) if you are burning so much memory in a single function that this becomes a concern, you're doing it wrong.

Unless there's a compelling reason for explicitly limiting scope like this, and I don't see a good one, I would recommend against it. If nothing else, it's rarely used and so could confuse other programmers/later maintainers, either if they (like you) hadn't seen it before or if they couldn't figure out why you did it.

In Java (unlike C) you are allowed to declare the variable inside the for loop if you are worried about scope.

For example:

for (int i = 0; i < number; i++) {
    //Do Something
}

For the while loop, you can always change it to a Do-While if you are worried about the variable living outside the scope of the method.

Example:

do {
    int i = 0;
    //Do Something

    i++;
} while (i < number);

Comment: I see no reason why you can't/shouldn't do what you had. Personally, I would execute the while loop in the same method, but would change the for loop to reflect what I have.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top