Java Fields and Methods

In our previous post on how to get started in Java, we learned how to set up our machine and install JDK and IDE. In this tutorial, we will learn what are java fields and methods and how to create one.

Java Classes contains different fields and methods. Fields hold specific data while the methods contain codes that manipulate the data inside the fields.

Java Fields

Fields hold data. Fields can be a Primitive Type or a Reference Type. There are 8 primitive types in java, which are int, byte, long, short, double, float, boolean, and char. To define a field, we need the type of field, the name of the field, and the value of that field. For example:

     
int num = 1;

In the above example, we have a field that is an integer, with the name num and holds the value 1. If you will not define a value for the field, it will contain its default value. For primitive types, the default values are tabulated below.

TypeDefault Value
int0
byte0
short0
long0L
float0.0f
double0.0d
char ‘u0000’
booleanfalse

You can read more about the primitive types in java in Oracle site using this link: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html.

Reference Types are data types that are created in java. The String type is a reference type and all reference types are initially have null values.

There are different types of fields or variables. Here we will discuss 3 of them which are the Local Variables, Global Variables, and Parameters

Local variables are defined inside the method, thus, it can only be accessed inside the method from which it was defined. Unlike local variables, global variables can be called by any methods. Global variables mostly are defined at the top of the class file after defining the class name. Parameters are variables that were included upon calling the method. You will learn more about the parameters later.

Java Methods

Methods are codes that manipulate the value of fields or performs different operations. To create a method, one must define the name of the method plus opening and closing parenthesis and brackets. A sample method looks like below:

public void addNum(){
     //insert code here
}

Methods can also be defined with parameters. A sample method with parameters is just like below:

public void addNum(int firstNum, int secondNum) {
     //insert code here
}

In the above codes, there two parameters that were defined. First is an integer with the name firstNum and second is also an integer with its name secondNum. Parameters can be used inside the method just like any variables. You can call a method by using its name. If it doesn’t have any parameters declared, then you can call the method by just the name plus parenthesis like addNum().

If it has a parameter, you should include the fields or directly type the parameters by calling its name like addNum(5,3). If the method is in another class, you can call it by className.methodName().

By convention, all variables or fields and also methods should be in camelcase format. That is the first word should start with a small letter and the second word should start in a capital letter. Also, the first word of the name of the method should be a verb. The field name should be a word or combined words which are the similar name of objects in the real world.

For example, the addNum method starts its name with a verb add, and the firstNum field is more likely can be described that it was the first number. It is much easier to understand than defining fields with one-letter names such as x, which is the common name of a variable.

You should be familiar with using fields and methods because these are fundamentals in creating programs. Below are examples of methods and fields.

Example

public class Main {
     int firstNum = 5;        //these are global variables.
     int secondNum = 10;      //they can be called by any method.

     public void addNum(){
          int answer = 0;     //this is a local variable.
          answer = firstNum + secondNum;
          System.out.println("The sum of two numbers is: " + answer);
     }

     public static void main(String args[]){
          Main mains = new Main();
          mains.addNum();
     }
}

In our example, we have a class named Main. Inside the Main Class, we have global variables firstNum and secondNum which have the values 5 and 10 respectively. Also, inside our class, we have defined a method named addNum() and our main method.

Inside our main method, we have first initialized our class:

Main mains = new Main()

It means that we have initialized the class Main and referenced it using the variable mains.

After that, we have called the addNum() method, which can be called using className.methodName() or in our example, it was called using:

mains.addNum();

The program will then go to the method and process whatever has been written inside. In our implementation, we have created a local variable answer which has been initialized with a value of zero. The below code will then be called.

answer = firstNum + secondNum;

And by substituting the values, it translates to:

answer = 5 + 10;

The sum 15 will be stored in the answer variable.

Lastly, when the program read the method System.out.println(“The sum of two numbers is:” + answer) will give us an output of “The sum of two numbers is 15”.

Now we learn what are Java Fields and Methods. In our next tutorial, we will learn what are the Java Access Levels.

Share this tutorial!