Introduction to Java Static Methods – II




Welcome back! In this tutorial we are going to discuss more about the input parameters, return values and static member variables. These topics are essential for the full understanding of the methods.

Previously, I have explained you about the signature of a method. There, I have already mentioned you about the input parameters.

Eg:

Here, the one which is inside the brackets, grade is the input parameter.

This is similar to the declaration of a variable. So this grade is a variable of type String and it can be used inside the method block.

See, we have used the variable grade in the switch statement. But there’s one problem.

Although we declared the variable, we didn’t assigned a value such as “A”, “B” or any other String to it!

Now that assignment of the value is done when calling the method. Can you remember, in the main() method, we wrote,

There, when we calling the method, we actually pass a String variable to it. The value stored in inp is copied to the grade.

Now I think you can understand how important a method is. It simplifies our life. See the whole bunch of code that should be included in main method is replaced by just one line gradeDescription(inp).

Of course you can add more than one input parameter. Then the parameters are separated by commas.

Eg:

public static void testMethod(int a, String b, double c){

.
.
.

}

In this method, we have 3 input parameters. So if we want to call this, we have to enter 3 real values for this method. For example,

Return values

The return values are the outcome of a method. As I mentioned in the previous tutorial, the return values can be any type such as int, String, boolean etc.

How can you calculate the hypotenuse of a triangle when the width and the height of a triangle is given?

If x and y are width and height, hypotenuse equals to square root of x and y.

We can write this as a Java method as follows;

So if you want to print the hypotenuse of a triangle with width, height = 3 and 4, you can do this;

In the main() method, you can do this to call the method hypotenuse() and print the value;

Now let’s see the new features of this method hypotenuse().

First, instead of void, we have double. As I mentioned in the earlier tutorial, it’s a clue about the return type. So, this method will definitely output a double value.

The return statement at the end does the returning operation.

So once the method is executed, a double value is given out from the method.

This value can be assigned to a double variable. This the thing that is done in the main() method, with the statement,

So remember, if you promise to return any type in the signature, then definitely you are bonded to return such type in the end, using a return statement.

Static member variables

So far, we have declared variables only inside a particular method. Didn’t we? Those variables are called local variables.

Variable can also be placed outside a method. Those types of variables are called member variables.

Member variables can also be static or non-static. But here we are talking only about static member variables.

How did we declare a variable inside a method?

But member variables have extra parts.

Eg:

See, it has

<access modifiers> <static> <type> <variable name>

format.

Before using a local variable, we had to assign a value to it.

Eg:

But member variables already have default values when we declare them. For example,

integer member variable    :- default value = 0,

boolean member variable :- default value = false

String member variable      :- default value = null (it’s a special value. We will discuss this later)

That means, it’s not compulsory to assign a value to the variable. But if we want our own value instead of the default value, we can do that.

But notice, the declaration and the assignment of value is done at once. You can’t do this.

It is illegal to assign values for the member variables outside a method. If you want to assign values later, you have to do it inside a method.

So far, I couldn’t explain you the importance of a member variable. It is very important that member variables are visible to all the methods in the containing class.

The local variables that are used inside a method are not visible to an other method even in the same class.

So, member variables are a great way to communicate between two methods 🙂

OUTPUT:

0

Member variable x = 10

testMethod1, I know you were called before me 🙂

Member variable x = 234

Now in this Class, we have a static member variable x.

Since we didn’t set a value to it, the default value in x is 0. We can confirm it from the print statement in the main() method.

Now, we call the testMethod1(). It changes the value of the x to 10. As we didn’t add a custom value to x during declaration, the value of x can now only be changed inside a method.

After calling the testMethod2() it again changes the value of x to 234.

The important thing here is that x is visible to the testMethod1() as well as testMethod2(). Local variables can’t do that.

Now this advantage is used to detect whether the testMethod1() is executed before the testMethod2().

If the value of x is 10, that means, most probably, testMethod1() has called and it changed the value

(but you can also assign 10 to x inside the main() method as well. Then if you call testMethod2(), it will mistakenly identify that testMethod1() has called…ah this is for illustration purpose only mate 🙂

This is the end of the extended Java methods tutorial. Hope you have learnt some important things.

Our basic tutorial series ends here. But still a long way to go!! Be courageous! Let’s meet in the advanced learner course soon. See ya!! 🙂