Buzz Code
Published in

Buzz Code

Java 8 | Local/Field Variable and Method

Hey guys! Wow it’s been too long since I posted something! I’ve been too busy with working on my new Java project, which I’m going to post on here when it’s ready. So for today, I’m going to talk about the Method.


We’ve briefly saw what method is when we were practicing the String Method.

In that post, I told about how the method works, today I’ll show you how to declare and use the method.

Before that, let’s go back to the eclipse, and see what we already know about the method.

So this is the usual start that we know when we create the class in the Java project, that there is a main inside of the class. And usually we typed the code inside of the main block. That main block is also a method which the Java reads the first in that class. It means the main method is not the only method that we can put in the class. In that case we can get to do this.

Just like this, we can declare the new method! And there, void means it doesn’t have return value, and since I didn’t put anything in between of the parentheses it also doesn’t have a parameter.

So how to use it? It’s easy, you can just call the method!

Just like this, you can put the name of the method and put the parentheses and the colon at the end. You can just type all the codes in the main method, but people use the method to make the code more readable and fixable. And also you can define certain action in one method and call it as many times as you want!

[Local/Field Variable]

And there are two different types of the variable. First, the Local variable is the variable that you can only use in the method or a specific block of statement that you declared it. What does it mean? Do you remember the for loop?

You see, there is a variable i in the for loop, and that i is a local variable here, cause you can use this variable only in that for statement where you declared the i. After the for loop finishes the variable i goes away too. Also all the other variables that we’ve been declared in the main method, is also the local variables!

Then what is the Field variable? The field variable is the method declared on the outside of any methods. So it looks like below.

So the variables num and text are declared in the outside of the methods. These Field variables alive as long as the class is active.

Now let’s see the example of how to use them.

So in this example, I declared the field variable num and in the main method I declared the two local variables numA and numB each 10 and 40. And then I called the method named plus and put numA and numB as the parameters of the plus method. Because they are the local variables So you can’t use them in the outside of the main method. So you have to pass them as a parameter when you use them in another method.

On the other hand you can use num in any methods in the class since it’s a field variable. So at the end when you print the value of it out then you can see 50 on the console.

So this is all for today, and tomorrow I’ll bring some practice about it so we can get more familiar to it. Thank you for reading my post again guys! See ya!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store