Java Reference

Java Reference Home | Smart Soft Home

Code Block

Description

A code block refers to a block of code enclosed within curly braces. The code block starts with an open curly brace and ends with a closing curly brace. Examples incude a try block, a catch block, an if block, etc.

Where code blocks are used (or where curly braces are used)

Class code block
public class{
  ...
}
Method code block
public void print(){
  ...
}
If code block
if(x<5){
  ...
}

Note: If there is only one statement in the code block, the curly braces are optional.

if(x<5) this.jump();
Switch code block
switch(i){
  ...
}
While code block
while(x<5){
  ...
}

Note: If there is only one statement in the code block, the curly braces are optional.

while(x<5) System.out.println(x++);

For code block
for(int x=0;x<5;x++){
  ...
}

Note: If there is only one statement in the code block, the curly braces are optional.

for(int x=0;x<5;x++) System.out.println(x);
Do While code block
do{
  ...
}while (x<5);
Try Code Block
try{
  ...
}
Catch Code Block
catch(Exception ex){
  ...
}
Finally Code Block
finally{
  ...
}
Static Initializer Code Block
static{
  ...
}
Anonymous Code Block
{
  ...
}