7

I'm quite inexperienced in making GUI's with Swing and now I'm wondering if its possible to use "{" and "}" just to subdivide my code a little bit e.g.

[...]
JFrame f = new JFrame();
JPanel p = new JPanel();
{
   JLabel a = new JLabel("Hello");
   p.add(a);

   JLabel b = new JLabel("World!");
   p.add(b);
}
f.add(p);
[...]

I tested it and I don't think it made any difference... Am I wrong?

Thanks in advance

Marvin
  • 1,832
  • 2
  • 13
  • 22

3 Answers3

18

Yes, it's possible, you can use a block anywhere you can use an individual statement. Variables declared within that block will only be valid within the block. E.g.:

void method() {
    String allThisCodeCanSeeMe;

    // ...

    {
        String onlyThisBlockCanSeeMe;
        // ...
    }

    {
        String onlyThisSecondBlockCanSeeMe;
        // ...
    }

    // ....
}

But: Usually, if you find yourself wanting to do something like this, it suggests that you need to break the code into smaller functions/methods, and have what's currently your one method call those smaller parts:

void method() {
    String thisMethodCanSeeMe;

    // ...

    this.aSmallerMethod();        // <== Can pass in `thisMethodCanSeeMe`
    this.anotherSmallerMethod();  // <== if needed

    // ...
}

private void aSmallerMethod() {
    String onlyThisMethodCanSeeMe;
    // ...
}

private void anotherSmallerMethod() {
    String onlyThisSecondSmallerMethodCanSeeMe;
    // ...
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 3
    100 up-votes for the **`But:..."`** section. – Hovercraft Full Of Eels Dec 05 '15 at 17:38
  • 2
    @HovercraftFullOfEels: It took me at least 15 years of *professional work* to learn that lesson, despite people telling me (embarrassingly). To the extent I have. Should paste it to my monitor, I *still* find myself failing at it. – T.J. Crowder Dec 05 '15 at 17:41
  • 2
    @T.J.Crowder I see no justification to split a method into smaller ones if they are not independently usable. So failing at "it" isn't necessarily an indication of doing anything wrong. GUI initialization blocks naturally often become large and splitting them up into methods doesn't really help readability (instead of working trough a screen of initializer code you now have *two* screens of private methods to maintain *and* name). As appropiate as the advice *sometimes* is, its not a blanket recipe for improving code readability. – Durandal Dec 05 '15 at 18:34
  • 1
    @Durandal: I'm afraid you're flying in the face of a fair bit of research there, but to each their own. Yes, sometimes the names are mind-bogglingly dull, but it still helps to have things in discrete chunks. – T.J. Crowder Dec 05 '15 at 20:06
  • 1
    @Durandal one possible (and very important) reason is readability, where the top level method's behavior is now described as a smaller list of higher level statements, instead of a larger number of lower level statements. In one word: abstraction. – Juan Carlos Coto Dec 07 '15 at 01:50
4

The only difference the braces make is that any variables declared within those braces are invisible outside of them.

For the example:

JPanel p = new JPanel();
{
   JLabel a = new JLabel("Hello");
   p.add(a);

   int b = 5;
}

b = 10; // Compiler error
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
2

{} are a 'block' of code and adding more won't have any effect. Indenting is the accepted way to make stuff more readable. You can add more braces but you are not conforming to the way most of us write Java.

nicomp
  • 4,344
  • 4
  • 27
  • 60
  • As others wrote, you can limit scope of symbols by putting them in braces but it's not a standard practice at all. – nicomp Dec 05 '15 at 17:39