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;}
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.
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
xof type integer. Thestatickeyword is used to mark a variable as a static variable. - Line 3: We declare a static code block. Here, the
xvariable is initialized to10. - Line 9: The
main()method is declared. - Line 10: The
println()statement is used to print the value ofxto 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.
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 pointint x = 10;int y = 20;System.out.println(x + y);}}
Explanation
- Line 1: We create a class named
Main, which contains themain()method. - Line 2: We have a code block inside of the
main()method. - Line 5: We declare a variable
xand initialize it with the value 10. - Line 6: We declare a variable
yand initialize it with the value 20. - Line 8: We print the sum of
xandy.
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 executeSystem.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
xandy.
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 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);}}
Explanation
- Line 1: We create a class named
Main, which contains themain()method. - Line 3: We create the
printSum()method. This method has two parameters,xandy. - Line 5: We have a code block inside of the
printSum()method. This code block contains a single statement that prints out the sum ofxandy. - Line 9: We create the
main()method, which is the entry point for our program. - Line 16: The
main()method calls theprintSum()method. When theprintSum()method is called, the code in its code block is executed and the sum ofxandyis 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 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);}}}
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
xandy. - Line 13: The statement prints out the difference of
xandy.