Simple understanding of the definition of Java methods and the use of implementation details


This article mainly introduces a simple understanding of the definition of Java methods and the use of detailed explanation, this article through the example code is very detailed, for everyone’s study or work has a certain reference learning value, need friends can refer to

1. Concept

The “method” in Java language may also be called “function” in other languages. For some complex code logic, if you want to reuse the code and “use it at any time”, you can put the code in a brace “{}”, and give it a name. When using the code, you can directly find the name and call it

2. Actual combat drill

2.1 method of testing incoming parameters and return values


package cn.test.logan.day04;

import java.util.ArrayList;

public class MethodDemo {
  public int num1;
  public int num2;
  //Method: used to encapsulate a piece of logic
  //Syntax for defining methods: public return value type method name () {// logic}
  public int getSum() {
    int sum = num1 + num2;
    return sum;
  //Methods that have no return value are represented by void
  public void say() {
    System. Out. Println ("this is a method with no return value, ha ha");
  //A method with parameters but no return value has the same name, but the parameter list is different. It is called method overload
  public void say(String param) {
    System. Out. Println ("my input parameter is" + param ");
  //Method with multiple parameters and no return value
  public void say(String param1,String param2) {
    System. Out. Println ("the first input parameter is" + Param1 ");
    System. Out. Println ("the second input parameter is" + param2 ");
  //Methods with parameters and return values
  public int getDiff(int a,int b) {
    int diff = a - b;
    return diff;
  //Used to sort ArrayList
  public static void sortForArrayList(ArrayList<Integer> list) {
     for(int i=0;i<list.size()-1;i++) {
       for(int j=0;j<list.size()-1-i;j++) {
         if(list.get(j) > list.get(j+1)) {
           int tmp = list.get(j);
           list.set(j, list.get(j+1));
           list.set(j+1, tmp);
  //Print ArrayList
  public void print(ArrayList<Integer> list) {
    for(int i=0;i<list.size();i++) {

package cn.test.logan.day04;

import java.util.ArrayList;

public class MethodTest {
  public static void main(String[] args) {
    MethodDemo md = new MethodDemo();
    md.num1 = 12;
    md.num2 = 13;
    int sum = md.getSum();  
    System. Out. Println ("the sum of two numbers is" + sum ");
    //Call a method with no return value
    //Call a method with parameters and no return value
    MD. say ("parameter 1");
    //Call a method with multiple parameters and no return value
    MD. say ("parameter a", "parameter B");
    //Call the method with parameter and return value
    int diff = md.getDiff(20, 10);
    //Define an ArrayList, sort it in order of arrival, and print it
    ArrayList<Integer> al = new ArrayList<>();


2.2 test member method and static method

Member methods, also known as object methods, are related to objects and need to be called by objects.

Static methods, also known as class methods, have no relationship with objects and can be called directly by classes.


public class MethodDemo2 {
  public String name;
  //Define the method of a class (static method)
   *This method has nothing to do with the specific object
   *Such a method can be defined as a class method -- > a class method
   *Static methods can be called directly by class
  public static void print() {
  //Define a member method
  public void showName() {

package cn.test.logan.day04;

public class MethodTest2 {
  public static void main(String[] args) {
    //Static method call
    //Member method call
    MethodDemo2 m2 = new MethodDemo2();"Jack";

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

Learn a Linux command every day (62): Ping

Yesterday’s recommendation:Learn a Linux command every day (61): WGet Command introduction The ping command is used to test the network connectivity between hosts. Ping is a simple way to send network data to and receive network data from another computer on the network. It is usually used to test whether it can be accessed through […]