Java Methods


Methods are behaviors of a class. They are responsible for how a class interact with other classes or behave generally. Class is just a container that holds different methods. It is in methods that logic are written and all actions are executed.
Syntax of a java method.

protected void onCreate(Bundle savedInstanceState) {
   
super.onCreate(savedInstanceState);
    setContentView(R.layout.
activity_main);
}

A method must have an access modifier e.g. public, private, protected, default etc.
It must have a return type. If it is not meant to return anything, then you declare void as its return type. The example above has not return, thereby it is declared void.
Example of a method with return type

public String getMyLocation() {
   
return myLocation;
}

It must have a name. Your method must have a name. You are not permitted to use Java reserved words as a method name.
The first name or identifier must start with a lowercase. If there are concatenation of more than one name, the first name must be in lowercase and the first letter of the next word must be in upper case.

public String getMyLocation() {
   
return myLocation;
}

A method may have several numbers of arguments or no argument at all. The arguments are written in between the ( ). If a method contains more than one argument, the arguments should be separated by comma. If the arguments have the same data type, you must declare it separate as though they have different data types.

private int classGrades(int maths, int english){

   
return maths + english;
}
A java method is a collection of statements. Statements are written between two curly brackets {}.
protected void onCreate(Bundle savedInstanceState) {
   
//statements
   
super.onCreate(savedInstanceState);
    setContentView(R.layout.
activity_main);
}
 
Method Overloading

Method Overloading is when a class have two or more methods with the same name but different parameters. The only thing that is different in this methods is the parameters they take.
This first method is not taking in any parameter

public String getMyLocation() {
   
return myLocation;
}
The second method is taking in a parameter
public String getMyLocation(String myLocation) {
   
return myLocation;
}
 
In the previous post, we worked on button clicks. I want to show you another way of how to implement button clicks (click listener) in our app.
In your activity_main.xml file, add android:onClick="addBtn" 

<!--The button that is responsible for add numbers-->
<Button
   
android:id="@+id/add"
   
android:layout_width="wrap_content"
   
android:layout_height="wrap_content"
   
android:text="Add"
   
android:onClick="addBtn"/>
 
And in your MainActivity.java
Instead of this long process:
 
addButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {
          //Anytime the button is clicked 1 is added to the screen
              numberUpdate = numberUpdate + 1;

                //Displays the numberUpdate to the screen
                //if you don't set the text, nothing will be displayed to 
                //the screen
                defaultText.setText("" + numberUpdate);
            }
        });

Just use this
 
        //addBtn is the value of android:onClick value in the our xml
       
public void addBtn(View view) {
           
//Anytime the button is clicked 1 is added to the screen
           
numberUpdate = numberUpdate + 1;

           
//Displays the numberUpdate to the screen
            //if you don't set the text, nothing will be displayed to
            //the screen
           
defaultText.setText("" + numberUpdate);
        }