Blocks and Methods in Java
After learning about the data types in Java, this article is all about blocks and methods in Java. We will discuss the types of blocks and see how methods work in Java. By the end of this article, you will be able to write and call a method.
Contents
Blocks in Java
Block refers to a set of statements inside 2 curly braces (one opening ‘{“ and one closing “}” ). Java supports 2 types of blocks. They are:
-
Static Block
-
- If the block of code is declared with the static keyword, it is called Static Block in Java.
- We know that the main method is the point where JVM starts program execution. But the static block is always executed before the main method.
- A static block only executes for a single time for the life cycle of the program.
-
Non-static Block
-
- If you declare a block without any static keyword, then it is a Non-Static Block.
- Non-static blocks are executed before the constructor is called by the user.
- A non-static may execute n number of times as it depends upon the user.
package test; public class JavaBlocks { //a static block static { System.out.println("This is a static block"); } //constructor JavaBlocks(){ System.out.println("This is the constructor of JavaBlocks"); } //non static block { System.out.println("This is a non static block"); } public static void main(String[] args) { System.out.println("This is the main method 1"); JavaBlocks obj1=new JavaBlocks(); //creation of object of the class to instantiate the constructor JavaBlocks obj2=new JavaBlocks(); } }
Output:
In the above output, you can see that a static block is executed only once and before the main() method. But non-static block can be executed many numbers of times depending upon object creation.
Methods in Java
- A method in Java is a set of instructions that perform a specific task.
- It is a special type of block that has a name and a set of arguments.
- A method may or may not have a return type. Some methods return a value whereas some do not return any value.
- There are 2 types of methods in Java:
- The method must be called somewhere in the program in order to perform the task and provide the required return value.
- When a method is called, the control moves to the method and executes the statements inside its body sequentially. When the task is over, the control returns back to the original location and points to the next statement of the method call line.
Predefined Methods
- Predefined methods are already present in Java and readily available for use. The best examples are the main method and the print method.
public static void main(String[] args) {}
System.out.println();
- There are several predefined methods available for the String class, Math class, Array class, Integer class, etc.
User-defined methods
- The programmer can create and define methods as per his needs.
- Java provides this facility to support user-defined methods for an easier and better approach.
- The syntax to declare a user defined method in Java:
- access_modifier : It describes the scope of the method.
- return_type : This is the data type of the return value of the method. If the method does not return any value, then the return type must be void.
- method_name : This is the name of the method.
- list_of_arguements : This is the list of parameters define what type of variables the method will operate on. The type, order, and the number of parameters of a method differentiate.
Refer to the below program to have a clear understanding of user-defined methods.
package test; public class JavaMethods { //method returns a value of type int public int addition(int a, int b) { return (a+b); } //method returning no values- (void) datatype public void display(){ System.out.println("This method does not return any value."); } public static void main(String[] args) { JavaMethods obj1 = new JavaMethods(); /* the method addition is called and the return value is assigned to a variable of type int *which is same as the return type of the method. */ int add=obj1.addition(10, 30); System.out.println("The output of addition method is "+ add); /*The void method display is called. This does not return any value. So it cannot be assigned to any variable */ obj1.display(); } }
Output:
In this article, we saw how to define write and call methods and how to use blocks for performing various tasks in Java. The main significance of using methods is “reusing the methods”. You can write a method once and reuse it every single time you want to execute that functionality.
In my next article, I will give a detailed description of the packages in Java. We will learn about the different types of packages available. And what the packages hold within them.