Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

I wanted to fully understand why would a developer open up a code block?

When is a code block properly used?

1 Answer

Code blocks are used with classes, functions/methods, and control flow (it's possible I may be missing a use-case). They logically group one or more statements to be executed as a whole when the associated function or control statement is executed.

if (true) {
  // do this
  // and this
} else {
  // just do this
}

public void myFunction() {
  // excecute
  // all off this
  // when myFunction() is called
}