
Hey, welcome you to the java static methods tutorial. Now, this is a very VERY important lesson. Understanding the methods is compulsory for a programmer. So stay focused in this lesson 🙂
We just touched the methods in our previous tutorial Introduction to Java String –II. However, this tutorial will refresh that previous knowledge as well.
Now can you remember how we started writing a program?
In the previous lesson we made a class called ForLoop,
public class ForLoop {
public static void main(String[] args){
.
.
.
}
}
So now we are going to get an understanding about the 2nd statement. That is public static void main(String[] args)…
what is actually the public static void main thing? It’s what we call the main method. This is the place where we write some program so that it runs when the run button is pressed.
We are not permitted to write the code in the free space between the public class ForLoop{….. and public static void main(String[] args){…
OK. I’ll come back to this main method later. Right now what we should focus is the “method”.
What is a method??
It is a certain technique used by programming languages to minimize the repetition of code. A method is like a black-box. Or some kind of machine where you give a certain input and receive an output.
The code inside the black-box is responsible for the correct output when given an input. But this we don’t need to know this code in order to use the method…
Let’s take a code example. Can you remember the methods that we have used in String? (See Introduction to Java String –II)
For example, if name is a variable of type string,
name.toUpperCase()
is a method used to get a new String of the name in capital letters format.
You don’t know the inner working of this toUpperCase() method. Java developers have already written that for you.
All you need to do is to type the name of the method correctly and use it with a non-empty variable 🙂 This is exactly a black-box where you don’t know the inner working.
One other important thing. The code you write inside the black-box is reusable.
Taking the previous example of String method name.toUpperCase(). You can use the toUpperCase() method over and over again in any place of the code you use name variable.
You can of course write your own methods… Let’s see…
There are two types of methods.
- Static methods
- Non-static methods
The previous example name.toUpperCase() is actually, a non-static method. They are a bit complicated to understand fully right now.
So, first, let’s look at static methods.
The basic structure of a static method is as follows:
<access modifier> <static keyword> <output return type> <method name> (< input parameters>)
{
<statements>
}
🙁 what is the meaning of this?? Still got no idea. Right? Wait… We’ll see.
Let’s go back to our one of the previous examples. Can you remember we wrote a code for the grades of a certain exam using switch statements?
There, when you input your grade that you got in the exam, “A”, “B”, “C” or “F”, certain info about that grade is given out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.Scanner; public class SwitchStatements{ public static void main(String[] args) { Scanner userInput = new Scanner(System.in); System.out.print("Please enter the grade : "); String grade = userInput.next(); grade = grade.toUpperCase(); switch (grade) { case "A": System.out.println("Congratulations! Your marks is " + "in the range 100 - 75"); break; case "B": System.out.println("Your marks is in the range 50 - 75"); break; case "C": System.out.println("Your marks is in the range 35 - 50"); break; case "F": System.out.println("Sorry. Your score is not " + "enough to follow the course"); break; default: System.out.println("Error!"); System.out.println("The grade you entered is not valid"); } } } |
Here, we can organize this code in to a more easy-to-understand manner by using a static method.
Let’s take this switch statement separately to a method…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.Scanner; public class JavaMethods { public static void gradeDescription(String grade){ grade = grade.toUpperCase(); switch(grade){ case "A": System.out.println("Congratulations! Your marks is " + "in the range 100 - 75"); break; case "B": System.out.println("Your marks is in the range 50 - 75"); break; case "C": System.out.println("Your marks is in the range 35 - 50"); break; case "F": System.out.println("Sorry. Your score is not " + "enough to follow the course"); break; default: System.out.println("Error!"); System.out.println("The grade you entered is not valid"); } } public static void main(String[] args){ Scanner userInput = new Scanner(System.in); System.out.print("Please enter the grade : "); String inp = userInput.next(); gradeDescription(inp); } } |
Now see, we have two methods inside our class.
- gradeDescription() method
- main() method
Number one thing you must know is you can make methods only inside a class. You can’t create a method outside the class. See, in the example, both methods are written within the brackets of JavaMethods class.
Another point: You can see that the main() method has smaller number of lines than the previous one.
This makes the main() method easier to understand. The statement “gradeDescription(inp)”, in the main() method is known as the method call of the other method, gradeDescription().
At the method call, the computer jumps to the method gradeDescription() and start executing the lines in that method.
Now let’s focus on gradeDescription() method.
Look at the first letter of the method. Its a simple letter. NOT Capital.
It’s not a rule, but a convention among programmers.
So if you follow this, it will be really helpful for your peer programmers to understand what you have written.
Also, when you get used to it, you will also understand easily what others have written. So, get used to it 🙂
Now we’ll check what these public static void words mean.
The line
1 |
public static void main(String[] args) |
is called the signature of the method.
This signature gives the basic overview of the method.
That means, the basic info about the black-box.
- The Range of visibility of the method,
- whether it’s static or not,
- What is the output type?,
- the name of the method,
- what is the input type?
are the info given by this signature.
Let’s analyze one by one…
the word public is called the access modifier and it is used to make the method visible and accessible to other classes that import the class which contains the method.
Another access modifier is private. From the name itself, this modifier does not allow other classes to access the method. So you can use the method only inside the parent class.
By the way, there’s another one called protected. But let’s talk about that later 🙂
Next item is the word static. This will distinguish static methods from non-static. I’ll skip this right now. For now, think that it’s a compulsory part of a static method signature.
The keyword void is used to give an idea about the output of the method. The term void means that no any output is returned from the method.
Other than void there are many other return types. You have learnt about them before.
Examples include int, String, boolean, double,char… etc.
For example, if int is written as the return type, then definitely the output of the method will be a returning of an integer value.
We will discuss about this return type a bit deep in the next tutorial.
Next part in the method signature is the name of the method. Here, the name is gradeDescription. Remember, the first letter is written simple as a rule of thumb!
The last one of the signature is the input type within the brackets. It is also one of java types. This input is compulsory since otherwise, the method implementation will fail.
In the given example, if we don’t give the grade String to the method, then how can the method know which grade that the user has input and how can it respond??
So even trying to call the method without entering the input will cause an error.
You can check this by modifying the method call gradeDescription(inp) in the main() method into gradeDescription().
Well, I guess it’s enough for this tutorial. Let’s meet in the next one. There, I’ll extend this introduction to methods and tell you some cool facts… see ya!