Using methods

Basically, methods are blocks in our Java class. Let's write one block here as an example, and observe where the opened and closed brackets are placed. The following example shows one complete block:

public void getData()
{
static int a=4;
}

In this code, we have named the block of code getData() and void is the return type for this method.

If we are expecting to return a number from the method, and the number is an integer, then we have to write integer in place of void. The same applies with strings; if we are planning to return a string from the getData() method, then we have to declare it as a string. If we are not returning anything, that is, if we are simply writing a few lines of code, then we leave it as void.

Take a look at the following screenshot:

Return type is given as void for getData()

Here, we are not returning anything, so we keep it as void.

Let's add a return 2; line below System.out.println(" I am in method");. Here, we are returning a number that is an integer. That's why we will receive an error here. If we hover our mouse over the error shown over return 2;, you will see a suggestion, Change method return type to 'int':

Quick fixes drop down with suggestions to correct the code error

On clicking on the suggestion, our IDE automatically modifies the return type to integer and the error disappears. The same is also the case with the string data type.

We will discuss the public access modifier later on in Chapter 10The Importance of the final Keyword, Packages, and ModifiersThere is a lot to discuss since there are different access modifiers in Java, such as public, private, protected, and default. We will take a look at each access modifier with appropriate examples so that they are explained in detail. For now, let's just accept all access modifiers as public.

Now you must be wondering why these methods are present in Java. What is their use?

Let's say that we are executing a 10-line block of code, for example, to add two integers on a page. Now every time we reach a page that requires us to add two integers, we have to write the 10 lines of code again. Maybe replicating the 10 lines of code won't matter for one instance but what if we were to require this block of code for 10 instances throughout the entire project? So 10 pages and 10 lines of code makes 100 lines of code that we are replicating in a single Java program. So to avoid that, we write all the 10 lines of code into one block, and we name that block as, for example, getData or anything else. Thereafter, whenever we require the 10 lines of code that we typed, we can simply call the getData method. All the 10 lines of code will fall into that particular block, and it will get executed. In this case, we avoid writing the code 10 times; we write it only once in a method and call that method whenever it is required.

Let's explain this with an example:

package coreJavaTraining;

public class Firstclass {

public void getData()
{
System.out.println(" I am in method")
}
public static void main(String[] args) {
System.out.println(a);
System.out.println("hi");
System.out.println("hello world");
}
}

In the preceding class, we will consider " I am in method" as the 10 lines of code that we were talking about earlier. We want to call this method, but here the getData() block is outside the main block, which means that the code cannot be executed. To execute it, we must move it inside the main block. In most cases, people just copy the code inside the main block and then receive an error since no methods are allowed inside the main block. The method should be written outside the main block, but inside the class. If we write something outside the class, there is no point since Java does not catch it. But if we write the method outside the main block, how do we get it inside the main block? For that, we need to create an object for the class where our method is defined. Here, our methods are defined in the Firstclass class, so we create an object for this class and with that object we can access the methods and variables present in the class.

In the next section, we'll see what the objects are, where we use them, and how objects are used to call the methods and variables.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.218.215