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;}
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);}
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.
Static code blocks are a special type of code block that are used to initialize variables. Static code blocks are marked with the static
keyword. The initialization code is run only once when the class is first loaded.
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);}}
MyClass
. x
of type integer. The static
keyword is used to mark a variable as a static variable. x
variable is initialized to 10
.main()
method is declared. 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
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.
main()
code blockEvery Java program has at least one code block, which is the code block that contains the main()
method.
public class Main {public static void main(String[] args) {// this is the main code block// it contains the program's entry pointint x = 10;int y = 20;System.out.println(x + y);}}
Main
, which contains the main()
method.main()
method. x
and initialize it with the value 10.y
and initialize it with the value 20. 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
.
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.
public static void printSum(int x, int y) {// this is the method's code block// it contains the statements that the method will executeSystem.out.println(x + y);}
printSum()
method has a code block that contains a single statement. x
and y
. 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.
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 executeSystem.out.println(x + y);}public static void main(String[] args) {// this is the main methodint x = 10;int y = 20;// call the printSum() methodprintSum(x, y);}}
Main
, which contains the main()
method.printSum()
method. This method has two parameters, x
and y
.printSum()
method. This code block contains a single statement that prints out the sum of x
and y
.main()
method, which is the entry point for our program.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. You can also nest code blocks inside of other code blocks. Nested code blocks are often used to group related statements.
public class Main {public static void main(String[] args) {// this is the main methodint x = 10;int y = 20;// this code block is nested inside the main method{// print out the sum of x and ySystem.out.println(x + y);// print out the difference of x and ySystem.out.println(x - y);}}}
main()
method. This code block contains two statements. x
and y
.x
and y
.