• Fields and Methods

    In this tutorial, what are fields and methods and how to create one. Java Classes contains different fields and methods. Fields hold a specific data while the methods contains codes that manipulate the data inside the fields.

    Fields

    Fields holds 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 the 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 which 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.

    Type Default Value
    int 0
    byte 0
    short 0
    long 0L
    float 0.0f
    double 0.0d
    char ‘u0000’
    boolean false

    Reference Types are data types which 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 are defined at the top of the class file after defining the class name. Parameters are variables that was included upon calling the method. You will learn more about parameters later.

    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. The method looks like below:
    public void addNum(){
         //insert code here
    }
    Methods can also be define 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 doesn’t have parameters declared, 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 capital letter. Also, first word of the name of the method should be a verb. Field name should be word or combined words which are similiar name of objects in 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 name such as x, which is the common name of a variable.

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

    Example 1

    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();
         }
    }
    Explanation: In our example number 1, we have a class named Main. Inside the Main Class, we have global variables firstNum and secondNum which has the values 5 and 10 respectively. Also, inside our class, we have defined a method named addNum() and also our main method. Inside our main method, we have first initialized our class, Main mains = new Main(), which means that we have initialized the class Main and named it mains. After that, we have called the addNum(), which can be called using className.methodName() which is mains.addNum(). The program will then go to the method and process whatever has been written inside. Inside the method, we have a local variable answer which has been initialized with a value of zero. The line answer = firstNum + secondNum will then be called. Since firstNum and secondNum is a global variable, we can use them in any methods. That line will then be equal to answer = 5 + 10, which will give us 15 and will be save to the variable answer. At last, 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”.

    Related Post