How to define a code block in Java

Introduction: code block in Java

A code block is a section of code enclosed in curly braces {}. Code blocks are used to group one or more statements. Code blocks can be nested, meaning you can have code blocks inside of other code blocks.

The example below is a code block that contains a single statement:

{
int x = 10;
}
A code block enclosing a single statements

The code block in the code above contains a single statement, int x = 10;.

Code blocks usually contain multiple statements. The example below is a code block that contain multiple statements:

{
int x = 10;
int y = 20;
System.out.println(x + y);
}
A code block enclosing multiple statements

In the code example above, the code block is the section of code between the opening curly brace { and the closing curly brace }. In the above code block is used to group the int x = 10;, int y = 20;, and System.out.println(x + y); statements. When the code block is executed, the statements are run from top to bottom.

The static code block

Static code blocks are a special type of code block that are used to initialize variables. Static code blocks are marked with the statickeyword. The initialization code is run only once when the class is first loaded.

Code example

The following is an example of a static code block:

public class MyClass {
static int x;
static {
// This code is run only once
// when the class is first loaded.
x = 10;
}
public static void main(String[] args) {
System.out.print("x -> " + x);
}
}

Explanation

  • Line 1: We declare a class named MyClass.
  • Line 2: We declare a static variable x of type integer. The static keyword is used to mark a variable as a static variable.
  • Line 3: We declare a static code block. Here, the x variable is initialized to 10.
  • Line 9: The main() method is declared.
  • Line 10: The println() statement is used to print the value of x to the console.

When the above code is compiled and run, it will produce the following output:

x -> 10
The output of the static block code example

As you can see from the output, the value of x is 10. This is because the static code block is executed only once when the class is first loaded.

The main() code block

Every Java program has at least one code block, which is the code block that contains the main() method.

Code example

public class Main {
public static void main(String[] args) {
// this is the main code block
// it contains the program's entry point
int x = 10;
int y = 20;
System.out.println(x + y);
}
}

Explanation

  • Line 1: We create a class named Main, which contains the main() method.
  • Line 2: We have a code block inside of the main() method.
  • Line 5: We declare a variable x and initialize it with the value 10.
  • Line 6: We declare a variable y and initialize it with the value 20.
  • Line 8: We print the sum of x and y.

When the program is run, the code in the main() code block is executed. The output of the code would be 30.

Creating methods

Code blocks can also be used to create methods in Java. A method is a self-contained section of code that performs a specific task. When you create a method, you give it a name and specify the code block that contains the statements that the method will execute.

Code example

public static void printSum(int x, int y) {
// this is the method's code block
// it contains the statements that the method will execute
System.out.println(x + y);
}

Explanation

  • Line 1: The printSum() method has a code block that contains a single statement.
  • Line 4: This statement prints out the sum of the variables x and y.

Calling a method

You can call a method from another method, or from the main() method. When you call a method, the code in the method's code block is executed.

Code example

public class Main {
public static void printSum(int x, int y) {
// this is the method's code block
// it contains the statements that the method will execute
System.out.println(x + y);
}
public static void main(String[] args) {
// this is the main method
int x = 10;
int y = 20;
// call the printSum() method
printSum(x, y);
}
}

Explanation

  • Line 1: We create a class named Main, which contains the main() method.
  • Line 3: We create the printSum() method. This method has two parameters, x and y.
  • Line 5: We have a code block inside of the printSum() method. This code block contains a single statement that prints out the sum of x and y.
  • Line 9: We create the main() method, which is the entry point for our program.
  • Line 16: The main() method calls the printSum() method. When the printSum() method is called, the code in its code block is executed and the sum of x and y is printed out.

Nested code clocks

You can also nest code blocks inside of other code blocks. Nested code blocks are often used to group related statements.

Code example

public class Main {
public static void main(String[] args) {
// this is the main method
int x = 10;
int y = 20;
// this code block is nested inside the main method
{
// print out the sum of x and y
System.out.println(x + y);
// print out the difference of x and y
System.out.println(x - y);
}
}
}

Explanation

  • Line 8–14: There is a code block nested inside the main() method. This code block contains two statements.
  • Line 10: The statement prints out the sum of x and y .
  • Line 13: The statement prints out the difference of x and y.

Free Resources